+7
-2
.rustlings-state.txt
+7
-2
.rustlings-state.txt
+3
exercises/15_traits/traits1.rs
+3
exercises/15_traits/traits1.rs
+6
exercises/15_traits/traits2.rs
+6
exercises/15_traits/traits2.rs
···
4
4
5
5
// TODO: Implement the trait `AppendBar` for a vector of strings.
6
6
// `append_bar` should push the string "Bar" into the vector.
7
+
impl AppendBar for Vec<String> {
8
+
fn append_bar(mut self) -> Self {
9
+
self.push("Bar".to_string());
10
+
self
11
+
}
12
+
}
7
13
8
14
fn main() {
9
15
// You can optionally experiment here.
+3
-1
exercises/15_traits/traits3.rs
+3
-1
exercises/15_traits/traits3.rs
···
3
3
// implementors like the two structs below can share that default behavior
4
4
// without repeating the function.
5
5
// The default license information should be the string "Default license".
6
-
fn licensing_info(&self) -> String;
6
+
fn licensing_info(&self) -> String {
7
+
"Default license".to_string()
8
+
}
7
9
}
8
10
9
11
struct SomeSoftware {
+1
-1
exercises/15_traits/traits4.rs
+1
-1
exercises/15_traits/traits4.rs
···
11
11
impl Licensed for OtherSoftware {}
12
12
13
13
// TODO: Fix the compiler error by only changing the signature of this function.
14
-
fn compare_license_types(software1: ???, software2: ???) -> bool {
14
+
fn compare_license_types(software1: impl Licensed, software2: impl Licensed) -> bool {
15
15
software1.licensing_info() == software2.licensing_info()
16
16
}
17
17
+1
-1
exercises/15_traits/traits5.rs
+1
-1
exercises/15_traits/traits5.rs
···
19
19
impl OtherTrait for OtherStruct {}
20
20
21
21
// TODO: Fix the compiler error by only changing the signature of this function.
22
-
fn some_func(item: ???) -> bool {
22
+
fn some_func(item: impl SomeTrait + OtherTrait) -> bool {
23
23
item.some_function() && item.other_function()
24
24
}
25
25
+30
-2
solutions/15_traits/traits1.rs
+30
-2
solutions/15_traits/traits1.rs
···
1
+
// The trait `AppendBar` has only one function which appends "Bar" to any object
2
+
// implementing this trait.
3
+
trait AppendBar {
4
+
fn append_bar(self) -> Self;
5
+
}
6
+
7
+
impl AppendBar for String {
8
+
fn append_bar(self) -> Self {
9
+
self + "Bar"
10
+
}
11
+
}
12
+
1
13
fn main() {
2
-
// DON'T EDIT THIS SOLUTION FILE!
3
-
// It will be automatically filled after you finish the exercise.
14
+
let s = String::from("Foo");
15
+
let s = s.append_bar();
16
+
println!("s: {s}");
17
+
}
18
+
19
+
#[cfg(test)]
20
+
mod tests {
21
+
use super::*;
22
+
23
+
#[test]
24
+
fn is_foo_bar() {
25
+
assert_eq!(String::from("Foo").append_bar(), "FooBar");
26
+
}
27
+
28
+
#[test]
29
+
fn is_bar_bar() {
30
+
assert_eq!(String::from("").append_bar().append_bar(), "BarBar");
31
+
}
4
32
}
+25
-2
solutions/15_traits/traits2.rs
+25
-2
solutions/15_traits/traits2.rs
···
1
+
trait AppendBar {
2
+
fn append_bar(self) -> Self;
3
+
}
4
+
5
+
impl AppendBar for Vec<String> {
6
+
fn append_bar(mut self) -> Self {
7
+
// ^^^ this is important
8
+
self.push(String::from("Bar"));
9
+
self
10
+
}
11
+
}
12
+
1
13
fn main() {
2
-
// DON'T EDIT THIS SOLUTION FILE!
3
-
// It will be automatically filled after you finish the exercise.
14
+
// You can optionally experiment here.
15
+
}
16
+
17
+
#[cfg(test)]
18
+
mod tests {
19
+
use super::*;
20
+
21
+
#[test]
22
+
fn is_vec_pop_eq_bar() {
23
+
let mut foo = vec![String::from("Foo")].append_bar();
24
+
assert_eq!(foo.pop().unwrap(), "Bar");
25
+
assert_eq!(foo.pop().unwrap(), "Foo");
26
+
}
4
27
}
+34
-2
solutions/15_traits/traits3.rs
+34
-2
solutions/15_traits/traits3.rs
···
1
+
trait Licensed {
2
+
fn licensing_info(&self) -> String {
3
+
"Default license".to_string()
4
+
}
5
+
}
6
+
7
+
struct SomeSoftware {
8
+
version_number: i32,
9
+
}
10
+
11
+
struct OtherSoftware {
12
+
version_number: String,
13
+
}
14
+
15
+
impl Licensed for SomeSoftware {}
16
+
impl Licensed for OtherSoftware {}
17
+
1
18
fn main() {
2
-
// DON'T EDIT THIS SOLUTION FILE!
3
-
// It will be automatically filled after you finish the exercise.
19
+
// You can optionally experiment here.
20
+
}
21
+
22
+
#[cfg(test)]
23
+
mod tests {
24
+
use super::*;
25
+
26
+
#[test]
27
+
fn is_licensing_info_the_same() {
28
+
let licensing_info = "Default license";
29
+
let some_software = SomeSoftware { version_number: 1 };
30
+
let other_software = OtherSoftware {
31
+
version_number: "v2.0.0".to_string(),
32
+
};
33
+
assert_eq!(some_software.licensing_info(), licensing_info);
34
+
assert_eq!(other_software.licensing_info(), licensing_info);
35
+
}
4
36
}
+33
-2
solutions/15_traits/traits4.rs
+33
-2
solutions/15_traits/traits4.rs
···
1
+
trait Licensed {
2
+
fn licensing_info(&self) -> String {
3
+
"Default license".to_string()
4
+
}
5
+
}
6
+
7
+
struct SomeSoftware;
8
+
struct OtherSoftware;
9
+
10
+
impl Licensed for SomeSoftware {}
11
+
impl Licensed for OtherSoftware {}
12
+
13
+
fn compare_license_types(software1: impl Licensed, software2: impl Licensed) -> bool {
14
+
// ^^^^^^^^^^^^^ ^^^^^^^^^^^^^
15
+
software1.licensing_info() == software2.licensing_info()
16
+
}
17
+
1
18
fn main() {
2
-
// DON'T EDIT THIS SOLUTION FILE!
3
-
// It will be automatically filled after you finish the exercise.
19
+
// You can optionally experiment here.
20
+
}
21
+
22
+
#[cfg(test)]
23
+
mod tests {
24
+
use super::*;
25
+
26
+
#[test]
27
+
fn compare_license_information() {
28
+
assert!(compare_license_types(SomeSoftware, OtherSoftware));
29
+
}
30
+
31
+
#[test]
32
+
fn compare_license_information_backwards() {
33
+
assert!(compare_license_types(OtherSoftware, SomeSoftware));
34
+
}
4
35
}
+37
-2
solutions/15_traits/traits5.rs
+37
-2
solutions/15_traits/traits5.rs
···
1
+
trait SomeTrait {
2
+
fn some_function(&self) -> bool {
3
+
true
4
+
}
5
+
}
6
+
7
+
trait OtherTrait {
8
+
fn other_function(&self) -> bool {
9
+
true
10
+
}
11
+
}
12
+
13
+
struct SomeStruct;
14
+
impl SomeTrait for SomeStruct {}
15
+
impl OtherTrait for SomeStruct {}
16
+
17
+
struct OtherStruct;
18
+
impl SomeTrait for OtherStruct {}
19
+
impl OtherTrait for OtherStruct {}
20
+
21
+
fn some_func(item: impl SomeTrait + OtherTrait) -> bool {
22
+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
23
+
item.some_function() && item.other_function()
24
+
}
25
+
1
26
fn main() {
2
-
// DON'T EDIT THIS SOLUTION FILE!
3
-
// It will be automatically filled after you finish the exercise.
27
+
// You can optionally experiment here.
28
+
}
29
+
30
+
#[cfg(test)]
31
+
mod tests {
32
+
use super::*;
33
+
34
+
#[test]
35
+
fn test_some_func() {
36
+
assert!(some_func(SomeStruct));
37
+
assert!(some_func(OtherStruct));
38
+
}
4
39
}