use std::ops::Index;
struct AssociationList<K,V> {
- pairs: ~[AssociationPair<K,V>]
-}
+ pairs: Vec<AssociationPair<K,V>> }
+#[deriving(Clone)]
struct AssociationPair<K,V> {
key: K,
value: V
}
}
-impl<K:Eq,V:Copy> Index<K,V> for AssociationList<K,V> {
+impl<K:Eq,V:Clone> Index<K,V> for AssociationList<K,V> {
fn index(&self, index: &K) -> V {
- for self.pairs.iter().advance |pair| {
+ for pair in self.pairs.iter() {
if pair.key == *index {
- return copy pair.value;
+ return pair.value.clone();
}
}
- fail!("No value found for key: %?", index);
+ fail!("No value found for key: {:?}", index);
}
}
pub fn main() {
- let foo = ~"foo";
- let bar = ~"bar";
+ let foo = "foo".to_owned();
+ let bar = "bar".to_owned();
- let mut list = AssociationList {pairs: ~[]};
- list.push(copy foo, 22);
- list.push(copy bar, 44);
+ let mut list = AssociationList {pairs: Vec::new()};
+ list.push(foo.clone(), 22);
+ list.push(bar.clone(), 44);
assert!(list[foo] == 22)
assert!(list[bar] == 44)