How do I use Serde to serialize a HashMap with structs as keys to JSON?
问题 I want to serialize a HashMap with structs as keys: extern crate serde_json; // 1.0.22 #[macro_use] extern crate serde_derive; // 1.0.68 use std::collections::HashMap; fn main() { #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash)] struct Foo { x: u64, } #[derive(Serialize, Deserialize, Debug)] struct Bar { x: HashMap<Foo, f64>, } let mut p = Bar { x: HashMap::new() }; p.x.insert(Foo { x: 0 }, 0.0); let serialized = serde_json::to_string(&p).unwrap(); } This code compiles, but when