Skip to content

Instantly share code, notes, and snippets.

@cgcardona
Forked from rust-play/playground.rs
Last active November 7, 2019 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgcardona/e6dd657da12db4563f83181d50c5ee3b to your computer and use it in GitHub Desktop.
Save cgcardona/e6dd657da12db4563f83181d50c5ee3b to your computer and use it in GitHub Desktop.
Query a Rust HashMap by struct as key
// 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