Skip to content

Commit

Permalink
Update generics2.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
GURUSUJAN authored Dec 27, 2024
1 parent 26cf498 commit 16b4075
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions exercises/14_generics/generics2.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
// generics2.rs
//
// 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,
// Rewrite it using generics so that it supports wrapping ANY type.
//

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> {
pub fn new(value: T) -> Self {
Wrapper { value }
}
}

fn main() {
// You can optionally experiment here.
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn store_u32_in_wrapper() {
assert_eq!(Wrapper::new(42).value, 42);
assert_eq!(Wrapper::new(42u32).value, 42u32);
}

#[test]
fn store_string_in_wrapper() {
let x = Wrapper::new(String::from("Foo"));
assert_eq!(x.value, String::from("Foo"));
}

#[test]
fn store_str_in_wrapper() {
assert_eq!(Wrapper::new("Foo").value, "Foo");
fn store_f64_in_wrapper() {
assert_eq!(Wrapper::new(42.0_f64).value, 42.0_f64);
}
}

0 comments on commit 16b4075

Please sign in to comment.