Skip to content

Commit f43c6d7

Browse files
95swhitemanyinsects
authored andcommitted
feat: Add traits3.rs exercise
1 parent 9b66b2a commit f43c6d7

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

exercises/traits/traits3.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}

info.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,18 @@ what the result should look like!
704704
Vectors provide suitable methods for adding an element at the end. See
705705
the documentation at: https://doc.rust-lang.org/std/vec/struct.Vec.html"""
706706

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+
707719
# QUIZ 3
708720

709721
[[exercises]]

0 commit comments

Comments
 (0)