Skip to content

Instantly share code, notes, and snippets.

@gong023
Last active August 29, 2015 14:07
Show Gist options
  • Save gong023/132973f2d330e29fc889 to your computer and use it in GitHub Desktop.
Save gong023/132973f2d330e29fc889 to your computer and use it in GitHub Desktop.
use std::hash::Hash;
use std::collections::HashMap;
pub struct Hashing<K: Eq + Hash, V: Eq + Hash> {
x: HashMap<K, V>
}
impl<'a, K: Eq + Hash + Clone, V: Eq + Hash + Clone> Hashing<K, V> {
pub fn new(h: HashMap<K, V>) -> Hashing<K, V> {
Hashing { x: h }
}
pub fn pick(self, keys: Vec<K>) -> HashMap<K, V> {
let mut picked = HashMap::new();
for element in keys.iter() {
if self.x.contains_key(element) {
// ここで新しい lifetime を作る
let e = element.clone();
let a = self.x.get_copy(element);
picked.insert(e, a);
}
}
return picked;
}
}
fn main() {
let mut h = HashMap::new();
h.insert(1i, 2u);
h.insert(2i, 3u);
h.insert(3i, 4u);
Hashing::new(h).pick(vec!(1i, 2i));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment