summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rustlings/.rustlings-state.txt6
-rw-r--r--rustlings/exercises/14_generics/generics1.rs2
-rw-r--r--rustlings/exercises/14_generics/generics2.rs8
-rw-r--r--rustlings/solutions/14_generics/generics1.rs17
-rw-r--r--rustlings/solutions/14_generics/generics2.rs28
5 files changed, 50 insertions, 11 deletions
diff --git a/rustlings/.rustlings-state.txt b/rustlings/.rustlings-state.txt
index c3678cb..163f806 100644
--- a/rustlings/.rustlings-state.txt
+++ b/rustlings/.rustlings-state.txt
@@ -1,6 +1,6 @@
DON'T EDIT THIS FILE!
-generics1
+traits1
intro1
intro2
@@ -57,4 +57,6 @@ errors2
errors3
errors4
errors5
-errors6 \ No newline at end of file
+errors6
+generics1
+generics2 \ No newline at end of file
diff --git a/rustlings/exercises/14_generics/generics1.rs b/rustlings/exercises/14_generics/generics1.rs
index 87ed990..7e5571f 100644
--- a/rustlings/exercises/14_generics/generics1.rs
+++ b/rustlings/exercises/14_generics/generics1.rs
@@ -6,7 +6,7 @@ fn main() {
// TODO: Fix the compiler error by annotating the type of the vector
// `Vec<T>`. Choose `T` as some integer type that can be created from
// `u8` and `i8`.
- let mut numbers = Vec::new();
+ let mut numbers: Vec<i32> = Vec::new();
// Don't change the lines below.
let n1: u8 = 42;
diff --git a/rustlings/exercises/14_generics/generics2.rs b/rustlings/exercises/14_generics/generics2.rs
index 8908725..ba77625 100644
--- a/rustlings/exercises/14_generics/generics2.rs
+++ b/rustlings/exercises/14_generics/generics2.rs
@@ -1,12 +1,12 @@
// This powerful wrapper provides the ability to store a positive integer value.
// TODO: Rewrite it using a generic so that it supports wrapping ANY type.
-struct Wrapper {
- value: u32,
+struct Wrapper<T> {
+ value: T,
}
// TODO: Adapt the struct's implementation to be generic over the wrapped value.
-impl Wrapper {
- fn new(value: u32) -> Self {
+impl<T> Wrapper<T> {
+ fn new(value: T) -> Self {
Wrapper { value }
}
}
diff --git a/rustlings/solutions/14_generics/generics1.rs b/rustlings/solutions/14_generics/generics1.rs
index dcf2377..e2195fd 100644
--- a/rustlings/solutions/14_generics/generics1.rs
+++ b/rustlings/solutions/14_generics/generics1.rs
@@ -1,4 +1,17 @@
+// `Vec<T>` is generic over the type `T`. In most cases, the compiler is able to
+// infer `T`, for example after pushing a value with a concrete type to the vector.
+// But in this exercise, the compiler needs some help through a type annotation.
+
fn main() {
- // DON'T EDIT THIS SOLUTION FILE!
- // It will be automatically filled after you finish the exercise.
+ // `u8` and `i8` can both be converted to `i16`.
+ let mut numbers: Vec<i16> = Vec::new();
+ // ^^^^^^^^^^ added
+
+ // Don't change the lines below.
+ let n1: u8 = 42;
+ numbers.push(n1.into());
+ let n2: i8 = -1;
+ numbers.push(n2.into());
+
+ println!("{numbers:?}");
}
diff --git a/rustlings/solutions/14_generics/generics2.rs b/rustlings/solutions/14_generics/generics2.rs
index dcf2377..14f3f7a 100644
--- a/rustlings/solutions/14_generics/generics2.rs
+++ b/rustlings/solutions/14_generics/generics2.rs
@@ -1,4 +1,28 @@
+struct Wrapper<T> {
+ value: T,
+}
+
+impl<T> Wrapper<T> {
+ fn new(value: T) -> Self {
+ Wrapper { value }
+ }
+}
+
fn main() {
- // DON'T EDIT THIS SOLUTION FILE!
- // It will be automatically filled after you finish the exercise.
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn store_u32_in_wrapper() {
+ assert_eq!(Wrapper::new(42).value, 42);
+ }
+
+ #[test]
+ fn store_str_in_wrapper() {
+ assert_eq!(Wrapper::new("Foo").value, "Foo");
+ }
}