diff options
| author | bors <bors@rust-lang.org> | 2020-02-25 21:27:39 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2020-02-25 21:27:39 +0000 |
| commit | 78295ce92fa5842e3eb1a05979fd22c4cb8191e8 (patch) | |
| tree | 1e0c866e0641d7c4e50837b5418360cd9f9daf13 /exercises | |
| parent | 358fb473cd3ebf06085b58f8c7ff1f649ec6ec7a (diff) | |
| parent | dc84aacc65392172164b728813449ecda8c3b6e6 (diff) | |
Auto merge of #274 - sjmann:master, r=fmoko
chore: fixed merge conflicts from traits exercises added by s-marios
I hope this doesn't step on any toes but I wanted to try the traits exercises from #216 so I updated them to match the new structure with hints included in info.toml
Diffstat (limited to 'exercises')
| -rw-r--r-- | exercises/traits/traits1.rs | 44 | ||||
| -rw-r--r-- | exercises/traits/traits2.rs | 35 |
2 files changed, 79 insertions, 0 deletions
diff --git a/exercises/traits/traits1.rs b/exercises/traits/traits1.rs new file mode 100644 index 0000000..8253ef8 --- /dev/null +++ b/exercises/traits/traits1.rs @@ -0,0 +1,44 @@ +// 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 main() { + let s = String::from("Foo"); + let s = s.append_bar(); + println!("s: {}", s); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn is_FooBar() { + assert_eq!(String::from("Foo").append_bar(), String::from("FooBar")); + } + + #[test] + fn is_BarBar() { + assert_eq!( + String::from("").append_bar().append_bar(), + String::from("BarBar") + ); + } + +}
\ No newline at end of file diff --git a/exercises/traits/traits2.rs b/exercises/traits/traits2.rs new file mode 100644 index 0000000..7f5014d --- /dev/null +++ b/exercises/traits/traits2.rs @@ -0,0 +1,35 @@ +// 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 + + + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn is_vec_pop_eq_bar() { + let mut foo = vec![String::from("Foo")].append_bar(); + assert_eq!(foo.pop().unwrap(), String::from("Bar")); + assert_eq!(foo.pop().unwrap(), String::from("Foo")); + } + +} |
