File tree Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ // traits3.rs
2
+ //
3
+ // Your task is to implement the Licensed trait for
4
+ // both structures and have them return the same
5
+ // information without writing the same function twice.
6
+ //
7
+ // Consider what you can add to the Licensed trait.
8
+
9
+ // I AM NOT DONE
10
+
11
+ pub trait Licensed {
12
+ fn licensing_info ( & self ) -> String ;
13
+ }
14
+
15
+ struct SomeSoftware {
16
+ version_number : i32 ,
17
+ }
18
+
19
+ struct OtherSoftware {
20
+ version_number : String ,
21
+ }
22
+
23
+ impl Licensed for SomeSoftware { } // Don't edit this line
24
+ impl Licensed for OtherSoftware { } // Don't edit this line
25
+
26
+ #[ cfg( test) ]
27
+ mod tests {
28
+ use super :: * ;
29
+
30
+ #[ test]
31
+ fn is_licensing_info_the_same ( ) {
32
+ let licensing_info = String :: from ( "Some information" ) ;
33
+ let some_software = SomeSoftware { version_number : 1 } ;
34
+ let other_software = OtherSoftware {
35
+ version_number : "v2.0.0" . to_string ( ) ,
36
+ } ;
37
+ assert_eq ! ( some_software. licensing_info( ) , licensing_info) ;
38
+ assert_eq ! ( other_software. licensing_info( ) , licensing_info) ;
39
+ }
40
+ }
Original file line number Diff line number Diff line change @@ -704,6 +704,18 @@ what the result should look like!
704
704
Vectors provide suitable methods for adding an element at the end. See
705
705
the documentation at: https://doc.rust-lang.org/std/vec/struct.Vec.html"""
706
706
707
+ [[exercises ]]
708
+ name = " traits3"
709
+ path = " exercises/traits/traits3.rs"
710
+ mode = " test"
711
+ hint = """
712
+ Traits can have a default implementation for functions. Structs that implement
713
+ the trait can then use the default version of these functions if they choose not
714
+ implement the function themselves.
715
+
716
+ See the documentation at: https://doc.rust-lang.org/book/ch10-02-traits.html#default-implementations
717
+ """
718
+
707
719
# QUIZ 3
708
720
709
721
[[exercises ]]
You can’t perform that action at this time.
0 commit comments