From 6a31cb09bd461890007aba791e549b0879c9aad0 Mon Sep 17 00:00:00 2001 From: seth <yesifan66@gmail.com> Date: Wed, 23 Jun 2021 16:53:55 +0800 Subject: [PATCH] complete: traits --- exercises/traits/traits1.rs | 5 +++-- exercises/traits/traits2.rs | 9 +++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/exercises/traits/traits1.rs b/exercises/traits/traits1.rs index 2ef9e11..e861dad 100644 --- a/exercises/traits/traits1.rs +++ b/exercises/traits/traits1.rs @@ -8,14 +8,15 @@ // 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(self) -> Self{ + self + "Bar" + } } fn main() { diff --git a/exercises/traits/traits2.rs b/exercises/traits/traits2.rs index 916c3c4..a1fdd1a 100644 --- a/exercises/traits/traits2.rs +++ b/exercises/traits/traits2.rs @@ -10,13 +10,18 @@ // 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> { + //Add your code here + fn append_bar(mut self) -> Self{ + self.push(String::from("Bar")); + self + } +} #[cfg(test)] mod tests {