generics2.rs (668B)
1 // This powerful wrapper provides the ability to store a positive integer value. 2 // TODO: Rewrite it using a generic so that it supports wrapping ANY type. 3 struct Wrapper<T> { 4 value: T, 5 } 6 7 // TODO: Adapt the struct's implementation to be generic over the wrapped value. 8 impl<T> Wrapper<T> { 9 fn new(value: T) -> Self { 10 Wrapper { value } 11 } 12 } 13 14 fn main() { 15 // You can optionally experiment here. 16 } 17 18 #[cfg(test)] 19 mod tests { 20 use super::*; 21 22 #[test] 23 fn store_u32_in_wrapper() { 24 assert_eq!(Wrapper::new(42).value, 42); 25 } 26 27 #[test] 28 fn store_str_in_wrapper() { 29 assert_eq!(Wrapper::new("Foo").value, "Foo"); 30 } 31 }