mirror of
https://git.aramjonghu.dev/AramJonghu/rustlings.git
synced 2026-09-11 20:13:30 +02:00
feat: generics done
This commit is contained in:
@@ -6,7 +6,7 @@ fn main() {
|
|||||||
// TODO: Fix the compiler error by annotating the type of the vector
|
// 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
|
// `Vec<T>`. Choose `T` as some integer type that can be created from
|
||||||
// `u8` and `i8`.
|
// `u8` and `i8`.
|
||||||
let mut numbers = Vec::new();
|
let mut numbers: Vec<i16> = Vec::new();
|
||||||
|
|
||||||
// Don't change the lines below.
|
// Don't change the lines below.
|
||||||
let n1: u8 = 42;
|
let n1: u8 = 42;
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
// This powerful wrapper provides the ability to store a positive integer value.
|
// 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.
|
// TODO: Rewrite it using a generic so that it supports wrapping ANY type.
|
||||||
struct Wrapper {
|
struct Wrapper<T> {
|
||||||
value: u32,
|
value: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Adapt the struct's implementation to be generic over the wrapped value.
|
// TODO: Adapt the struct's implementation to be generic over the wrapped value.
|
||||||
impl Wrapper {
|
impl<T> Wrapper<T> {
|
||||||
fn new(value: u32) -> Self {
|
fn new(value: T) -> Self {
|
||||||
Wrapper { value }
|
Wrapper { value }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user