summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-06-27 12:23:33 +0200
committermo8it <mo8it@proton.me>2024-06-27 12:23:33 +0200
commitc0452d160b889b3686409820192797d9e9f9cad7 (patch)
treea2d958597268f396c401a619e7b72739479466d1
parentb4b7ae63ada9128d4798d301cfc757a60904c6b8 (diff)
traits4 solution
-rw-r--r--exercises/15_traits/traits4.rs27
-rw-r--r--rustlings-macros/info.toml5
-rw-r--r--solutions/15_traits/traits4.rs35
3 files changed, 45 insertions, 22 deletions
diff --git a/exercises/15_traits/traits4.rs b/exercises/15_traits/traits4.rs
index ed63f6e..80092a6 100644
--- a/exercises/15_traits/traits4.rs
+++ b/exercises/15_traits/traits4.rs
@@ -1,23 +1,18 @@
-// Your task is to replace the '??' sections so the code compiles.
-//
-// Don't change any line other than the marked one.
-
trait Licensed {
fn licensing_info(&self) -> String {
- "some information".to_string()
+ "Default license".to_string()
}
}
-struct SomeSoftware {}
-
-struct OtherSoftware {}
+struct SomeSoftware;
+struct OtherSoftware;
impl Licensed for SomeSoftware {}
impl Licensed for OtherSoftware {}
-// YOU MAY ONLY CHANGE THE NEXT LINE
-fn compare_license_types(software: ??, software_two: ??) -> bool {
- software.licensing_info() == software_two.licensing_info()
+// TODO: Fix the compiler error by only changing the signature of this function.
+fn compare_license_types(software1: ???, software2: ???) -> bool {
+ software1.licensing_info() == software2.licensing_info()
}
fn main() {
@@ -30,17 +25,11 @@ mod tests {
#[test]
fn compare_license_information() {
- let some_software = SomeSoftware {};
- let other_software = OtherSoftware {};
-
- assert!(compare_license_types(some_software, other_software));
+ assert!(compare_license_types(SomeSoftware, OtherSoftware));
}
#[test]
fn compare_license_information_backwards() {
- let some_software = SomeSoftware {};
- let other_software = OtherSoftware {};
-
- assert!(compare_license_types(other_software, some_software));
+ assert!(compare_license_types(OtherSoftware, SomeSoftware));
}
}
diff --git a/rustlings-macros/info.toml b/rustlings-macros/info.toml
index 92e440a..43ccdf8 100644
--- a/rustlings-macros/info.toml
+++ b/rustlings-macros/info.toml
@@ -784,9 +784,10 @@ name = "traits4"
dir = "15_traits"
hint = """
Instead of using concrete types as parameters you can use traits. Try replacing
-the '??' with 'impl [what goes here?]'
+`???` with `impl [what goes here?]`.
-See the documentation at: https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters"""
+Related section in The Book:
+https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters"""
[[exercises]]
name = "traits5"
diff --git a/solutions/15_traits/traits4.rs b/solutions/15_traits/traits4.rs
index 4e18198..78b5a11 100644
--- a/solutions/15_traits/traits4.rs
+++ b/solutions/15_traits/traits4.rs
@@ -1 +1,34 @@
-// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
+trait Licensed {
+ fn licensing_info(&self) -> String {
+ "Default license".to_string()
+ }
+}
+
+struct SomeSoftware;
+struct OtherSoftware;
+
+impl Licensed for SomeSoftware {}
+impl Licensed for OtherSoftware {}
+
+fn compare_license_types(software1: impl Licensed, software2: impl Licensed) -> bool {
+ software1.licensing_info() == software2.licensing_info()
+}
+
+fn main() {
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn compare_license_information() {
+ assert!(compare_license_types(SomeSoftware, OtherSoftware));
+ }
+
+ #[test]
+ fn compare_license_information_backwards() {
+ assert!(compare_license_types(OtherSoftware, SomeSoftware));
+ }
+}