Last active
August 29, 2015 14:00
-
-
Save compressed/2035bd8721f6b016b91c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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); | |
} | |
} |
This file contains hidden or 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
❯❯❯ 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