diff --git a/REVISITS.md b/REVISITS.md index 9b5116e3..05bb5649 100644 --- a/REVISITS.md +++ b/REVISITS.md @@ -23,4 +23,9 @@ return results to new Vec vector 22) trait3 -> very important for understanding default implementation and overrides. Also reread this page: https://doc.rust-lang.org/book/ch10-02-traits.html#default-implementations. Answer also here: https://users.rust-lang.org/t/rustlings-traits3-rs-question/80742/3 25) trait5 -> important to understand trait bounds with generics. Also be sure to understand the short format vs. clearer format and pros/cons 26) Retake quiz3 for more traits+generics practice +27) lifetimes1 +28) standard library types iterators2 -> VERY IMPORTANT to understand map, chain/chain::>, join +29) iterator4 to make sure you understand fold/rfold + +clippy conversions macros standard_library_types threads diff --git a/exercises/lifetimes/lifetimes1.rs b/exercises/lifetimes/lifetimes1.rs index 0236470d..a12d3f5b 100644 --- a/exercises/lifetimes/lifetimes1.rs +++ b/exercises/lifetimes/lifetimes1.rs @@ -7,9 +7,7 @@ // // Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - -fn longest(x: &str, y: &str) -> &str { +fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { if x.len() > y.len() { x } else { diff --git a/exercises/lifetimes/lifetimes2.rs b/exercises/lifetimes/lifetimes2.rs index b48feabc..067c2318 100644 --- a/exercises/lifetimes/lifetimes2.rs +++ b/exercises/lifetimes/lifetimes2.rs @@ -6,8 +6,6 @@ // // Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { if x.len() > y.len() { x @@ -22,6 +20,7 @@ fn main() { { let string2 = String::from("xyz"); result = longest(string1.as_str(), string2.as_str()); + println!("The longest string is '{}'", result); } - println!("The longest string is '{}'", result); + } diff --git a/exercises/lifetimes/lifetimes3.rs b/exercises/lifetimes/lifetimes3.rs index ea483708..c0818f01 100644 --- a/exercises/lifetimes/lifetimes3.rs +++ b/exercises/lifetimes/lifetimes3.rs @@ -4,11 +4,9 @@ // // Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - -struct Book { - author: &str, - title: &str, +struct Book<'a> { + author: &'a str, + title: &'a str, } fn main() { diff --git a/exercises/standard_library_types/iterators1.rs b/exercises/standard_library_types/iterators1.rs index 0379c6bb..544dc675 100644 --- a/exercises/standard_library_types/iterators1.rs +++ b/exercises/standard_library_types/iterators1.rs @@ -8,17 +8,15 @@ // // Execute `rustlings hint iterators1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn main () { let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"]; - let mut my_iterable_fav_fruits = ???; // TODO: Step 1 + let mut my_iterable_fav_fruits = my_fav_fruits.iter(); // TODO: Step 1 assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana")); - assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2 + assert_eq!(my_iterable_fav_fruits.next(), Some(&"custard apple")); // TODO: Step 2 assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado")); - assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 3 + assert_eq!(my_iterable_fav_fruits.next(), Some(&"peach")); // TODO: Step 3 assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry")); - assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 4 + assert_eq!(my_iterable_fav_fruits.next(), None); // TODO: Step 4 } diff --git a/exercises/standard_library_types/iterators2.rs b/exercises/standard_library_types/iterators2.rs index 29c53afb..d3059028 100644 --- a/exercises/standard_library_types/iterators2.rs +++ b/exercises/standard_library_types/iterators2.rs @@ -3,8 +3,6 @@ // can offer. Follow the steps to complete the exercise. // Execute `rustlings hint iterators2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - // Step 1. // Complete the `capitalize_first` function. // "hello" -> "Hello" @@ -12,7 +10,7 @@ pub fn capitalize_first(input: &str) -> String { let mut c = input.chars(); match c.next() { None => String::new(), - Some(first) => ???, + Some(first) => first.to_uppercase().chain(c).collect(), } } @@ -21,7 +19,7 @@ pub fn capitalize_first(input: &str) -> String { // Return a vector of strings. // ["hello", "world"] -> ["Hello", "World"] pub fn capitalize_words_vector(words: &[&str]) -> Vec { - vec![] + words.iter().map(|word| capitalize_first(word)).collect() } // Step 3. @@ -29,7 +27,7 @@ pub fn capitalize_words_vector(words: &[&str]) -> Vec { // Return a single string. // ["hello", " ", "world"] -> "Hello World" pub fn capitalize_words_string(words: &[&str]) -> String { - String::new() + capitalize_words_vector(words).join("") } #[cfg(test)] diff --git a/exercises/standard_library_types/iterators3.rs b/exercises/standard_library_types/iterators3.rs index c97a6258..f4db4e34 100644 --- a/exercises/standard_library_types/iterators3.rs +++ b/exercises/standard_library_types/iterators3.rs @@ -6,8 +6,6 @@ // list_of_results functions. // Execute `rustlings hint iterators3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - #[derive(Debug, PartialEq, Eq)] pub enum DivisionError { NotDivisible(NotDivisibleError), @@ -23,21 +21,30 @@ pub struct NotDivisibleError { // Calculate `a` divided by `b` if `a` is evenly divisible by `b`. // Otherwise, return a suitable error. pub fn divide(a: i32, b: i32) -> Result { - todo!(); + if b == 0 { + return Err(DivisionError::DivideByZero); + } + + match a%b{ + 0 => Ok(a/b), + _ => Err(DivisionError::NotDivisible(NotDivisibleError{dividend:a, divisor:b})) + } } // Complete the function and return a value of the correct type so the test passes. // Desired output: Ok([1, 11, 1426, 3]) -fn result_with_list() -> () { +fn result_with_list() -> Result, DivisionError> { let numbers = vec![27, 297, 38502, 81]; - let division_results = numbers.into_iter().map(|n| divide(n, 27)); + let division_results: Vec = numbers.into_iter().map(|n| divide(n, 27).unwrap()).collect(); + Ok(division_results) } // Complete the function and return a value of the correct type so the test passes. // Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)] -fn list_of_results() -> () { +fn list_of_results() -> Vec> { let numbers = vec![27, 297, 38502, 81]; - let division_results = numbers.into_iter().map(|n| divide(n, 27)); + let division_results: Vec> = numbers.into_iter().map(|n| divide(n, 27)).collect(); + division_results } #[cfg(test)] diff --git a/exercises/standard_library_types/iterators4.rs b/exercises/standard_library_types/iterators4.rs index a02470ec..fb7e5207 100644 --- a/exercises/standard_library_types/iterators4.rs +++ b/exercises/standard_library_types/iterators4.rs @@ -1,8 +1,6 @@ // iterators4.rs // Execute `rustlings hint iterators4` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - pub fn factorial(num: u64) -> u64 { // Complete this function to return the factorial of num // Do not use: @@ -13,6 +11,7 @@ pub fn factorial(num: u64) -> u64 { // For an extra challenge, don't use: // - recursion // Execute `rustlings hint iterators4` for hints. + (1..=num).fold(1, |acc, x| acc * x) } #[cfg(test)] diff --git a/exercises/tests/tests2.rs b/exercises/tests/tests2.rs index a5ac15b1..99d5b400 100644 --- a/exercises/tests/tests2.rs +++ b/exercises/tests/tests2.rs @@ -3,12 +3,10 @@ // pass! Make the test fail! // Execute `rustlings hint tests2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - #[cfg(test)] mod tests { #[test] fn you_can_assert_eq() { - assert_eq!(); + assert_eq!("some words here", "some words here"); } } diff --git a/exercises/tests/tests3.rs b/exercises/tests/tests3.rs index 196a81a0..f82d4a43 100644 --- a/exercises/tests/tests3.rs +++ b/exercises/tests/tests3.rs @@ -4,7 +4,6 @@ // we expect to get when we call `is_even(5)`. // Execute `rustlings hint tests3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE pub fn is_even(num: i32) -> bool { num % 2 == 0 @@ -16,11 +15,11 @@ mod tests { #[test] fn is_true_when_even() { - assert!(); + assert!(is_even(10)); } #[test] fn is_false_when_odd() { - assert!(); + assert_eq!(false, is_even(5)); } }