Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created December 5, 2019 20:13
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 rust-play/dcb43521a1ad2c77b3ce9b91cdb312a2 to your computer and use it in GitHub Desktop.
Save rust-play/dcb43521a1ad2c77b3ce9b91cdb312a2 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::collections::HashMap;
struct Foo {
d: HashMap<String, f32>
}
impl Foo {
fn get(&mut self, s: &str) -> Option<&f32> {
if !self.d.contains_key(s) {
self.d.insert(s.to_string(), 0.0);
}
self.d.get(s)
}
}
fn main() {
let mut foo = Foo {
d: HashMap::new()
};
let mut saved = Vec::new();
for _i in 1..3 {
let l = foo.get("a");
saved.push(l);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment