summaryrefslogtreecommitdiff
path: root/rustlings/exercises
diff options
context:
space:
mode:
Diffstat (limited to 'rustlings/exercises')
-rw-r--r--rustlings/exercises/15_traits/traits1.rs3
-rw-r--r--rustlings/exercises/15_traits/traits2.rs8
-rw-r--r--rustlings/exercises/15_traits/traits3.rs4
-rw-r--r--rustlings/exercises/15_traits/traits4.rs2
-rw-r--r--rustlings/exercises/15_traits/traits5.rs2
-rw-r--r--rustlings/exercises/quizzes/quiz3.rs6
6 files changed, 19 insertions, 6 deletions
diff --git a/rustlings/exercises/15_traits/traits1.rs b/rustlings/exercises/15_traits/traits1.rs
index 85be17e..fad6a50 100644
--- a/rustlings/exercises/15_traits/traits1.rs
+++ b/rustlings/exercises/15_traits/traits1.rs
@@ -6,6 +6,9 @@ trait AppendBar {
impl AppendBar for String {
// TODO: Implement `AppendBar` for the type `String`.
+ fn append_bar(self) -> Self {
+ self + "Bar"
+ }
}
fn main() {
diff --git a/rustlings/exercises/15_traits/traits2.rs b/rustlings/exercises/15_traits/traits2.rs
index d724dc2..5dfc36d 100644
--- a/rustlings/exercises/15_traits/traits2.rs
+++ b/rustlings/exercises/15_traits/traits2.rs
@@ -5,6 +5,14 @@ trait AppendBar {
// TODO: Implement the trait `AppendBar` for a vector of strings.
// `append_bar` should push the string "Bar" into the vector.
+impl AppendBar for Vec<String> {
+ fn append_bar(self) -> Self {
+ let mut s = self.clone();
+ s.push(String::from("Bar"));
+ s
+ }
+}
+
fn main() {
// You can optionally experiment here.
}
diff --git a/rustlings/exercises/15_traits/traits3.rs b/rustlings/exercises/15_traits/traits3.rs
index c244650..144ac0a 100644
--- a/rustlings/exercises/15_traits/traits3.rs
+++ b/rustlings/exercises/15_traits/traits3.rs
@@ -3,7 +3,9 @@ trait Licensed {
// implementors like the two structs below can share that default behavior
// without repeating the function.
// The default license information should be the string "Default license".
- fn licensing_info(&self) -> String;
+ fn licensing_info(&self) -> String {
+ String::from("Default license")
+ }
}
struct SomeSoftware {
diff --git a/rustlings/exercises/15_traits/traits4.rs b/rustlings/exercises/15_traits/traits4.rs
index 80092a6..b23dcdd 100644
--- a/rustlings/exercises/15_traits/traits4.rs
+++ b/rustlings/exercises/15_traits/traits4.rs
@@ -11,7 +11,7 @@ impl Licensed for SomeSoftware {}
impl Licensed for OtherSoftware {}
// TODO: Fix the compiler error by only changing the signature of this function.
-fn compare_license_types(software1: ???, software2: ???) -> bool {
+fn compare_license_types(software1: impl Licensed, software2: impl Licensed) -> bool {
software1.licensing_info() == software2.licensing_info()
}
diff --git a/rustlings/exercises/15_traits/traits5.rs b/rustlings/exercises/15_traits/traits5.rs
index 5b356ac..6548014 100644
--- a/rustlings/exercises/15_traits/traits5.rs
+++ b/rustlings/exercises/15_traits/traits5.rs
@@ -19,7 +19,7 @@ impl SomeTrait for OtherStruct {}
impl OtherTrait for OtherStruct {}
// TODO: Fix the compiler error by only changing the signature of this function.
-fn some_func(item: ???) -> bool {
+fn some_func<T : SomeTrait + OtherTrait>(item: T) -> bool {
item.some_function() && item.other_function()
}
diff --git a/rustlings/exercises/quizzes/quiz3.rs b/rustlings/exercises/quizzes/quiz3.rs
index c877c5f..5bb859f 100644
--- a/rustlings/exercises/quizzes/quiz3.rs
+++ b/rustlings/exercises/quizzes/quiz3.rs
@@ -12,14 +12,14 @@
// block to support alphabetical report cards in addition to numerical ones.
// TODO: Adjust the struct as described above.
-struct ReportCard {
- grade: f32,
+struct ReportCard<T> {
+ grade: T,
student_name: String,
student_age: u8,
}
// TODO: Adjust the impl block as described above.
-impl ReportCard {
+impl<T: std::fmt::Display> ReportCard<T> {
fn print(&self) -> String {
format!(
"{} ({}) - achieved a grade of {}",