-
-
Save cgcardona/e6dd657da12db4563f83181d50c5ee3b to your computer and use it in GitHub Desktop.
Query a Rust HashMap by struct as key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// get by struct | |
use std::collections::HashSet; | |
#[derive(Hash, Eq, PartialEq, Debug)] | |
struct City { | |
name: String, | |
population: usize, | |
} | |
fn main() { | |
let mut cities = HashSet::new(); | |
cities.insert(City { | |
name: "San Francisco".to_string(), | |
population: 884_363, | |
}); | |
cities.insert(City { | |
name: "Tokyo".to_string(), | |
population: 9_273_000, | |
}); | |
cities.insert(City { | |
name: "Hong Kong".to_string(), | |
population: 7_392_000, | |
}); | |
println!( | |
"{:#?}", | |
&cities.get(&City { | |
name: "San Francisco".to_string(), | |
population: 884_363 | |
}) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment