diff --git a/src/assignments/assignment03.rs b/src/assignments/assignment03.rs index 93e1b10..f30136a 100644 --- a/src/assignments/assignment03.rs +++ b/src/assignments/assignment03.rs @@ -43,12 +43,43 @@ pub enum MyOption { MyNone, } -/// Maps the inner value if the given value is `MySome`; returns `MyNone` otherwise. +/// Maps an `MyOption` to `MyOption` by applying a function to a contained value. +/// +/// # Examples +/// +/// Converts an `MyOption` into an `MyOption`, consuming the original: +/// +/// ``` +/// fn len(s: String) -> usize { +/// s.len() +/// } +/// +/// assert_eq!(my_map(MyOption::MySome(String::from("Hello, World!")), len), MyOption::MySome(13)); +/// assert_eq!(my_map(MyOption::MyNone, len), MyOption::MyNone); +/// ``` pub fn my_map U>(v: MyOption, f: F) -> MyOption { todo!() } -/// Maps the inner value if the given value is `MySome`, but with a different type of function; returns `MyNone` otherwise. +/// Returns `MyNone` if the option is `MyNone`, otherwise calls `f` with the wrapped value and returns the result. +/// +/// Some languages call this operation flatmap. +/// +/// # Examples +/// +/// ``` +/// fn pos_then_to_string(x: isize) -> MyOption { +/// if x > 0 { +/// MyOption::MySome(x.to_string()) +/// } else { +/// MyOption::MyNone +/// } +/// } +/// +/// assert_eq!(my_and_then(MyOption::MySome(2), pos_then_to_string), MyOption::MySome(2.to_string())); +/// assert_eq!(my_and_then(MyOption::MySome(-3), pos_then_to_string), MyOption::MyNone); +/// assert_eq!(my_and_then(MyOption::MyNone, pos_then_to_string), MyOption::MyNone); +/// ``` pub fn my_and_then MyOption>(v: MyOption, f: F) -> MyOption { todo!() }