diff --git a/src/graphemes.rs b/src/graphemes.rs index e818e59..c9d749f 100644 --- a/src/graphemes.rs +++ b/src/graphemes.rs @@ -1,5 +1,6 @@ use unicode_segmentation::UnicodeSegmentation; +/// Trait extension example pub trait Trim { fn trim_count(&self, count: usize) -> String; } diff --git a/src/mutate_in_closure.rs b/src/mutate_in_closure.rs index fc1a0f4..70f4e46 100644 --- a/src/mutate_in_closure.rs +++ b/src/mutate_in_closure.rs @@ -1,24 +1,18 @@ -use std::cell::RefCell; - -/// Use ``RefCell`` to mutate a variable in a closure -/// # Panics -/// -pub fn mutate_in_closure() { - let my_string = RefCell::new("hello".to_string()); - let mutate = |new_val| { - *my_string.borrow_mut() = new_val; - }; - // mutate in closure - mutate("hej".to_string()); - assert_eq!(*my_string.borrow(), "hej"); - - *my_string.borrow_mut() = "bonjour".to_string(); - assert_eq!(*my_string.borrow(), "bonjour"); -} - +#[cfg(test)] mod tests { + use std::cell::RefCell; + /// Use ``RefCell`` to mutate a variable in a closure #[test] fn test_mutate_in_closure() { - super::mutate_in_closure(); + let my_string = RefCell::new("hello".to_string()); + let mutate = |new_val| { + *my_string.borrow_mut() = new_val; + }; + // mutate in closure + mutate("hej".to_string()); + assert_eq!(*my_string.borrow(), "hej"); + + *my_string.borrow_mut() = "bonjour".to_string(); + assert_eq!(*my_string.borrow(), "bonjour"); } } diff --git a/src/vec_any.rs b/src/vec_any.rs index 20a5e21..4b4c939 100644 --- a/src/vec_any.rs +++ b/src/vec_any.rs @@ -1,6 +1,6 @@ use core::any::Any; -trait AsAny { +pub trait AsAny { fn as_any(&self) -> &dyn Any; } impl AsAny for T { @@ -13,8 +13,8 @@ pub trait Animal { fn talk(&self); } -struct Cat {} -struct Dog { +pub struct Cat {} +pub struct Dog { pub name: String, } @@ -29,30 +29,27 @@ impl Animal for Dog { } } -trait AnyAnimal: Animal + AsAny {} +pub trait AnyAnimal: Animal + AsAny {} impl AnyAnimal for T {} -type BoxedAnimal = Box; +pub type BoxedAnimal = Box; -/// Demonstrates creating a vector of common Animal trait objects, -/// then downcasting to a concrete Dog type instance to be able to access its fields. -/// # Panics -/// -pub fn downcast_concrete_type_instance_from_trait_object() { - let c = Cat {}; - let d = Dog { name: "Fido".to_string() }; +#[cfg(test)] +mod tests { + use super::*; - let the_zoo = [Box::new(c) as BoxedAnimal, Box::new(d) as BoxedAnimal]; + /// Demonstrates creating a vector of common Animal trait objects, + /// then downcasting to a concrete Dog type instance to be able to access its fields. + #[test] + fn test_downcast_concrete_type_instance_from_trait_object() { + let c = Cat {}; + let d = Dog { name: "Fido".to_string() }; - the_zoo.iter().for_each(|a| a.talk()); + let the_zoo = [Box::new(c) as BoxedAnimal, Box::new(d) as BoxedAnimal]; - let x = &the_zoo[1]; - let dog = x.as_any().downcast_ref::().unwrap(); - assert!(dog.name == "Fido"); -} + the_zoo.iter().for_each(|a| a.talk()); -mod tests { - #[test] - fn test_cats_and_dogs() { - super::downcast_concrete_type_instance_from_trait_object(); + let x = &the_zoo[1]; + let dog = x.as_any().downcast_ref::().unwrap(); + assert!(dog.name == "Fido"); } }