2 // Test overloading of the `[]` operator. In particular test that it
3 // takes its argument *by reference*.
7 struct AssociationList<K,V> {
8 pairs: Vec<AssociationPair<K,V>> }
11 struct AssociationPair<K,V> {
16 impl<K,V> AssociationList<K,V> {
17 fn push(&mut self, key: K, value: V) {
18 self.pairs.push(AssociationPair {key: key, value: value});
22 impl<'a, K: PartialEq + std::fmt::Debug, V:Clone> Index<&'a K> for AssociationList<K,V> {
25 fn index(&self, index: &K) -> &V {
26 for pair in &self.pairs {
27 if pair.key == *index {
31 panic!("No value found for key: {:?}", index);
36 let foo = "foo".to_string();
37 let bar = "bar".to_string();
39 let mut list = AssociationList {pairs: Vec::new()};
40 list.push(foo.clone(), 22);
41 list.push(bar.clone(), 44);
43 assert_eq!(list[&foo], 22);
44 assert_eq!(list[&bar], 44);
46 assert_eq!(list[&foo], 22);
47 assert_eq!(list[&bar], 44);