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