Crates.io | iterator_to_hash_map |
lib.rs | iterator_to_hash_map |
version | 0.9.0 |
source | src |
created_at | 2016-01-13 07:07:55.217773 |
updated_at | 2016-01-13 07:07:55.217773 |
description | A Rust crate that adds a method to any `Iterator` or `IntoIterator` (such as `Vec`) that converts it to a `HashMap` using the trait `ToHashMap`. |
homepage | |
repository | https://github.com/bradurani/rust-iterator-to-hash-map |
max_upload_size | |
id | 3887 |
size | 4,039 |
A Rust crate that adds a method to any Iterator
or IntoIterator
(such as Vec
) that converts it to a HashMap
using the trait ToHashMap
.
extern crate iterator_to_hash_map;
use std::collections::HashMap;
use iterator_to_hash_map::ToHashMap;
struct Person {
id: i32,
first_name: &'static str,
last_name: &'static str,
}
let brad = Person {
id: 1,
first_name: "Brad",
last_name: "Urani",
};
let barb = Person {
id: 2,
first_name: "Barb",
last_name: "Hanover",
};
let a = vec![brad, barb];
let key_func = |i: &Person| -> i32 { i.id };
let value_func = |i: &Person| -> String {
i.first_name.to_string() + &" " + &i.last_name.to_string()
};
let map = a.to_hash_map(key_func, value_func);
# { 1: "Brad Urani", 2: "Barb Hanover" }
Open a pull request!