File tree Expand file tree Collapse file tree 3 files changed +101
-0
lines changed Expand file tree Collapse file tree 3 files changed +101
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -586,6 +586,28 @@ multiply the values into a mutable variable. Or you might write code more
586
586
functionally with recursion and a match clause. But you can also use ranges
587
587
and iterators to solve this in rust."""
588
588
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
+
589
611
# THREADS
590
612
591
613
[[exercises ]]
You can’t perform that action at this time.
0 commit comments