Skip to content

Commit 78295ce

Browse files
committed
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
2 parents 358fb47 + dc84aac commit 78295ce

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

exercises/traits/traits1.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// traits1.rs
2+
// Time to implement some traits!
3+
//
4+
// Your task is to implement the trait
5+
// `AppendBar' for the type `String'.
6+
//
7+
// The trait AppendBar has only one function,
8+
// which appends "Bar" to any object
9+
// implementing this trait.
10+
11+
// I AM NOT DONE
12+
trait AppendBar {
13+
fn append_bar(self) -> Self;
14+
}
15+
16+
impl AppendBar for String {
17+
//Add your code here
18+
19+
}
20+
21+
fn main() {
22+
let s = String::from("Foo");
23+
let s = s.append_bar();
24+
println!("s: {}", s);
25+
}
26+
27+
#[cfg(test)]
28+
mod tests {
29+
use super::*;
30+
31+
#[test]
32+
fn is_FooBar() {
33+
assert_eq!(String::from("Foo").append_bar(), String::from("FooBar"));
34+
}
35+
36+
#[test]
37+
fn is_BarBar() {
38+
assert_eq!(
39+
String::from("").append_bar().append_bar(),
40+
String::from("BarBar")
41+
);
42+
}
43+
44+
}

exercises/traits/traits2.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// traits2.rs
2+
//
3+
// Your task is to implement the trait
4+
// `AppendBar' for a vector of strings.
5+
//
6+
// To implement this trait, consider for
7+
// a moment what it means to 'append "Bar"'
8+
// to a vector of strings.
9+
//
10+
// No boiler plate code this time,
11+
// you can do this!
12+
13+
// I AM NOT DONE
14+
15+
trait AppendBar {
16+
fn append_bar(self) -> Self;
17+
}
18+
19+
//TODO: Add your code here
20+
21+
22+
23+
24+
#[cfg(test)]
25+
mod tests {
26+
use super::*;
27+
28+
#[test]
29+
fn is_vec_pop_eq_bar() {
30+
let mut foo = vec![String::from("Foo")].append_bar();
31+
assert_eq!(foo.pop().unwrap(), String::from("Bar"));
32+
assert_eq!(foo.pop().unwrap(), String::from("Foo"));
33+
}
34+
35+
}

info.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,28 @@ multiply the values into a mutable variable. Or you might write code more
586586
functionally with recursion and a match clause. But you can also use ranges
587587
and iterators to solve this in rust."""
588588

589+
# TRAITS
590+
591+
[[exercises]]
592+
name = "traits1"
593+
path = "exercises/traits/traits1.rs"
594+
mode = "test"
595+
hint = """
596+
A discussion about Traits in Rust can be found at:
597+
https://doc.rust-lang.org/1.30.0/book/second-edition/ch10-02-traits.html
598+
"""
599+
600+
[[exercises]]
601+
name = "traits2"
602+
path = "exercises/traits/traits2.rs"
603+
mode = "test"
604+
hint = """
605+
Notice how the trait takes ownership of 'self',and returns `Self'.
606+
Try mutating the incoming string vector.
607+
608+
Vectors provide suitable methods for adding an element at the end. See
609+
the documentation at: https://doc.rust-lang.org/std/vec/struct.Vec.html"""
610+
589611
# THREADS
590612

591613
[[exercises]]

0 commit comments

Comments
 (0)