Skip to content

Instantly share code, notes, and snippets.

@compressed
Last active August 29, 2015 14:00
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 compressed/2035bd8721f6b016b91c to your computer and use it in GitHub Desktop.
Save compressed/2035bd8721f6b016b91c to your computer and use it in GitHub Desktop.
extern crate collections;
use collections::HashMap;
struct Item {
id: int,
name: StrBuf,
}
fn build_hash(vec: Vec<Item>) -> HashMap<int, Item> {
let mut h = HashMap::new();
for item in vec.move_iter() {
h.insert(item.id, item);
}
return h;
}
fn main() {
let i1 = Item {id: 1, name: StrBuf::from_str("name")};
let i2 = Item {id: 2, name: StrBuf::from_str("name 2")};
let vec = vec![i1, i2];
let h = build_hash(vec);
for (k, v) in h.iter() {
println!("({:?}, {})", *k, v.name);
}
}
❯❯❯ rustc hash.rs && ./hash
(2, name 2)
(1, name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment