traits2.rs (534B)
1 trait AppendBar { 2 fn append_bar(self) -> Self; 3 } 4 5 impl AppendBar for Vec<String> { 6 fn append_bar(mut self) -> Self { 7 // ^^^ this is important 8 self.push(String::from("Bar")); 9 self 10 } 11 } 12 13 fn main() { 14 // You can optionally experiment here. 15 } 16 17 #[cfg(test)] 18 mod tests { 19 use super::*; 20 21 #[test] 22 fn is_vec_pop_eq_bar() { 23 let mut foo = vec![String::from("Foo")].append_bar(); 24 assert_eq!(foo.pop().unwrap(), "Bar"); 25 assert_eq!(foo.pop().unwrap(), "Foo"); 26 } 27 }