Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Forked from anonymous/playground.rs
Created January 9, 2018 04:28
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 whatalnk/8a11c7dc82e77de962e4052df760c95e to your computer and use it in GitHub Desktop.
Save whatalnk/8a11c7dc82e77de962e4052df760c95e to your computer and use it in GitHub Desktop.
Rust code shared from the playground
use std::collections::HashMap;
use std::collections::hash_map::Entry;
fn main() {
let mut d = HashMap::new();
match d.entry("k1") {
Entry::Vacant(e) => {
e.insert(Vec::new());
}
Entry::Occupied(mut e) => {
e.get_mut().push("v1");
}
}
match d.entry("k1") {
Entry::Vacant(e) => {
e.insert(Vec::new());
}
Entry::Occupied(mut e) => {
e.get_mut().push("v1");
}
}
println!("{:?}", d);
// {"k1": ["v1"]}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment