···6677impl AppendBar for String {
88 // TODO: Implement `AppendBar` for the type `String`.
99+ fn append_bar(self) -> Self {
1010+ format!("{self}Bar")
1111+ }
912}
10131114fn main() {
+6
exercises/15_traits/traits2.rs
···4455// TODO: Implement the trait `AppendBar` for a vector of strings.
66// `append_bar` should push the string "Bar" into the vector.
77+impl AppendBar for Vec<String> {
88+ fn append_bar(mut self) -> Self {
99+ self.push("Bar".to_string());
1010+ self
1111+ }
1212+}
713814fn main() {
915 // You can optionally experiment here.
+3-1
exercises/15_traits/traits3.rs
···33 // implementors like the two structs below can share that default behavior
44 // without repeating the function.
55 // The default license information should be the string "Default license".
66- fn licensing_info(&self) -> String;
66+ fn licensing_info(&self) -> String {
77+ "Default license".to_string()
88+ }
79}
810911struct SomeSoftware {
+1-1
exercises/15_traits/traits4.rs
···1111impl Licensed for OtherSoftware {}
12121313// TODO: Fix the compiler error by only changing the signature of this function.
1414-fn compare_license_types(software1: ???, software2: ???) -> bool {
1414+fn compare_license_types(software1: impl Licensed, software2: impl Licensed) -> bool {
1515 software1.licensing_info() == software2.licensing_info()
1616}
1717
+1-1
exercises/15_traits/traits5.rs
···1919impl OtherTrait for OtherStruct {}
20202121// TODO: Fix the compiler error by only changing the signature of this function.
2222-fn some_func(item: ???) -> bool {
2222+fn some_func(item: impl SomeTrait + OtherTrait) -> bool {
2323 item.some_function() && item.other_function()
2424}
2525
+30-2
solutions/15_traits/traits1.rs
···11+// The trait `AppendBar` has only one function which appends "Bar" to any object
22+// implementing this trait.
33+trait AppendBar {
44+ fn append_bar(self) -> Self;
55+}
66+77+impl AppendBar for String {
88+ fn append_bar(self) -> Self {
99+ self + "Bar"
1010+ }
1111+}
1212+113fn main() {
22- // DON'T EDIT THIS SOLUTION FILE!
33- // It will be automatically filled after you finish the exercise.
1414+ let s = String::from("Foo");
1515+ let s = s.append_bar();
1616+ println!("s: {s}");
1717+}
1818+1919+#[cfg(test)]
2020+mod tests {
2121+ use super::*;
2222+2323+ #[test]
2424+ fn is_foo_bar() {
2525+ assert_eq!(String::from("Foo").append_bar(), "FooBar");
2626+ }
2727+2828+ #[test]
2929+ fn is_bar_bar() {
3030+ assert_eq!(String::from("").append_bar().append_bar(), "BarBar");
3131+ }
432}
+25-2
solutions/15_traits/traits2.rs
···11+trait AppendBar {
22+ fn append_bar(self) -> Self;
33+}
44+55+impl AppendBar for Vec<String> {
66+ fn append_bar(mut self) -> Self {
77+ // ^^^ this is important
88+ self.push(String::from("Bar"));
99+ self
1010+ }
1111+}
1212+113fn main() {
22- // DON'T EDIT THIS SOLUTION FILE!
33- // It will be automatically filled after you finish the exercise.
1414+ // You can optionally experiment here.
1515+}
1616+1717+#[cfg(test)]
1818+mod tests {
1919+ use super::*;
2020+2121+ #[test]
2222+ fn is_vec_pop_eq_bar() {
2323+ let mut foo = vec![String::from("Foo")].append_bar();
2424+ assert_eq!(foo.pop().unwrap(), "Bar");
2525+ assert_eq!(foo.pop().unwrap(), "Foo");
2626+ }
427}
+34-2
solutions/15_traits/traits3.rs
···11+trait Licensed {
22+ fn licensing_info(&self) -> String {
33+ "Default license".to_string()
44+ }
55+}
66+77+struct SomeSoftware {
88+ version_number: i32,
99+}
1010+1111+struct OtherSoftware {
1212+ version_number: String,
1313+}
1414+1515+impl Licensed for SomeSoftware {}
1616+impl Licensed for OtherSoftware {}
1717+118fn main() {
22- // DON'T EDIT THIS SOLUTION FILE!
33- // It will be automatically filled after you finish the exercise.
1919+ // You can optionally experiment here.
2020+}
2121+2222+#[cfg(test)]
2323+mod tests {
2424+ use super::*;
2525+2626+ #[test]
2727+ fn is_licensing_info_the_same() {
2828+ let licensing_info = "Default license";
2929+ let some_software = SomeSoftware { version_number: 1 };
3030+ let other_software = OtherSoftware {
3131+ version_number: "v2.0.0".to_string(),
3232+ };
3333+ assert_eq!(some_software.licensing_info(), licensing_info);
3434+ assert_eq!(other_software.licensing_info(), licensing_info);
3535+ }
436}
+33-2
solutions/15_traits/traits4.rs
···11+trait Licensed {
22+ fn licensing_info(&self) -> String {
33+ "Default license".to_string()
44+ }
55+}
66+77+struct SomeSoftware;
88+struct OtherSoftware;
99+1010+impl Licensed for SomeSoftware {}
1111+impl Licensed for OtherSoftware {}
1212+1313+fn compare_license_types(software1: impl Licensed, software2: impl Licensed) -> bool {
1414+ // ^^^^^^^^^^^^^ ^^^^^^^^^^^^^
1515+ software1.licensing_info() == software2.licensing_info()
1616+}
1717+118fn main() {
22- // DON'T EDIT THIS SOLUTION FILE!
33- // It will be automatically filled after you finish the exercise.
1919+ // You can optionally experiment here.
2020+}
2121+2222+#[cfg(test)]
2323+mod tests {
2424+ use super::*;
2525+2626+ #[test]
2727+ fn compare_license_information() {
2828+ assert!(compare_license_types(SomeSoftware, OtherSoftware));
2929+ }
3030+3131+ #[test]
3232+ fn compare_license_information_backwards() {
3333+ assert!(compare_license_types(OtherSoftware, SomeSoftware));
3434+ }
435}
+37-2
solutions/15_traits/traits5.rs
···11+trait SomeTrait {
22+ fn some_function(&self) -> bool {
33+ true
44+ }
55+}
66+77+trait OtherTrait {
88+ fn other_function(&self) -> bool {
99+ true
1010+ }
1111+}
1212+1313+struct SomeStruct;
1414+impl SomeTrait for SomeStruct {}
1515+impl OtherTrait for SomeStruct {}
1616+1717+struct OtherStruct;
1818+impl SomeTrait for OtherStruct {}
1919+impl OtherTrait for OtherStruct {}
2020+2121+fn some_func(item: impl SomeTrait + OtherTrait) -> bool {
2222+ // ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2323+ item.some_function() && item.other_function()
2424+}
2525+126fn main() {
22- // DON'T EDIT THIS SOLUTION FILE!
33- // It will be automatically filled after you finish the exercise.
2727+ // You can optionally experiment here.
2828+}
2929+3030+#[cfg(test)]
3131+mod tests {
3232+ use super::*;
3333+3434+ #[test]
3535+ fn test_some_func() {
3636+ assert!(some_func(SomeStruct));
3737+ assert!(some_func(OtherStruct));
3838+ }
439}