diff --git a/exercises/traits/traits1.rs b/exercises/traits/traits1.rs index 8253ef8..76552a9 100644 --- a/exercises/traits/traits1.rs +++ b/exercises/traits/traits1.rs @@ -1,21 +1,22 @@ // traits1.rs // Time to implement some traits! -// +// // Your task is to implement the trait // `AppendBar' for the type `String'. -// +// // The trait AppendBar has only one function, // which appends "Bar" to any object // implementing this trait. -// I AM NOT DONE trait AppendBar { fn append_bar(self) -> Self; } impl AppendBar for String { - //Add your code here - + fn append_bar(mut self) -> Self { + self.push_str("Bar"); + self + } } fn main() { @@ -40,5 +41,4 @@ mod tests { String::from("BarBar") ); } - -} \ No newline at end of file +} diff --git a/exercises/traits/traits2.rs b/exercises/traits/traits2.rs index 7f5014d..67af43f 100644 --- a/exercises/traits/traits2.rs +++ b/exercises/traits/traits2.rs @@ -1,25 +1,25 @@ // traits2.rs -// +// // Your task is to implement the trait // `AppendBar' for a vector of strings. -// +// // To implement this trait, consider for // a moment what it means to 'append "Bar"' // to a vector of strings. -// +// // No boiler plate code this time, // you can do this! -// I AM NOT DONE - trait AppendBar { fn append_bar(self) -> Self; } -//TODO: Add your code here - - - +impl AppendBar for Vec<String> { + fn append_bar(mut self) -> Self { + self.push("Bar".to_string()); + self + } +} #[cfg(test)] mod tests { @@ -31,5 +31,4 @@ mod tests { assert_eq!(foo.pop().unwrap(), String::from("Bar")); assert_eq!(foo.pop().unwrap(), String::from("Foo")); } - }