From 1230b6cdb2fe4e034813765f49468b82147f5c31 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Sat, 27 May 2023 03:57:29 -0500 Subject: [PATCH] Current Progress --- exercises/iterators/iterators1.rs | 9 ++++----- exercises/iterators/iterators2.rs | 11 +++++++---- exercises/iterators/iterators3.rs | 15 +++++++++------ exercises/iterators/iterators4.rs | 1 + exercises/lifetimes/lifetimes1.rs | 3 +-- exercises/lifetimes/lifetimes2.rs | 3 +-- exercises/lifetimes/lifetimes3.rs | 7 +++---- exercises/quiz3.rs | 9 ++++----- exercises/tests/tests1.rs | 3 +-- exercises/tests/tests2.rs | 3 +-- exercises/tests/tests3.rs | 5 ++--- exercises/traits/traits3.rs | 6 +++--- exercises/traits/traits4.rs | 4 +--- exercises/traits/traits5.rs | 3 +-- 14 files changed, 39 insertions(+), 43 deletions(-) diff --git a/exercises/iterators/iterators1.rs b/exercises/iterators/iterators1.rs index f9cc3b3..588394d 100644 --- a/exercises/iterators/iterators1.rs +++ b/exercises/iterators/iterators1.rs @@ -8,17 +8,16 @@ // // 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/iterators/iterators2.rs b/exercises/iterators/iterators2.rs index 29c53af..eeee270 100644 --- a/exercises/iterators/iterators2.rs +++ b/exercises/iterators/iterators2.rs @@ -3,7 +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. @@ -12,7 +11,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_string().to_uppercase() + c.as_str(), } } @@ -21,7 +20,11 @@ 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![] + let mut combined = vec![]; + for word in words.iter() { + combined.push(capitalize_first(word)); + } + combined } // Step 3. @@ -29,7 +32,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/iterators/iterators3.rs b/exercises/iterators/iterators3.rs index c97a625..eb51d2a 100644 --- a/exercises/iterators/iterators3.rs +++ b/exercises/iterators/iterators3.rs @@ -6,7 +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 { @@ -23,21 +22,25 @@ 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); } + if (a%b) != 0 { return Err(DivisionError::NotDivisible(NotDivisibleError{dividend: a, divisor: b})); } + Ok(a/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 = numbers.into_iter().map(|n| divide(n, 27)).collect::, DivisionError>>(); + 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 = numbers.into_iter().map(|n| divide(n, 27)).collect::>>(); + division_results } #[cfg(test)] diff --git a/exercises/iterators/iterators4.rs b/exercises/iterators/iterators4.rs index a02470e..b0d97e2 100644 --- a/exercises/iterators/iterators4.rs +++ b/exercises/iterators/iterators4.rs @@ -13,6 +13,7 @@ pub fn factorial(num: u64) -> u64 { // For an extra challenge, don't use: // - recursion // Execute `rustlings hint iterators4` for hints. + vec![1..num].iter().rfold(1, |acc, x| acc + x) } #[cfg(test)] diff --git a/exercises/lifetimes/lifetimes1.rs b/exercises/lifetimes/lifetimes1.rs index 0236470..bd39d98 100644 --- a/exercises/lifetimes/lifetimes1.rs +++ b/exercises/lifetimes/lifetimes1.rs @@ -7,9 +7,8 @@ // // 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 b48feab..4d44d92 100644 --- a/exercises/lifetimes/lifetimes2.rs +++ b/exercises/lifetimes/lifetimes2.rs @@ -6,7 +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() { @@ -19,8 +18,8 @@ fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { fn main() { let string1 = String::from("long string is long"); let result; + let string2 = String::from("xyz"); { - let string2 = String::from("xyz"); result = longest(string1.as_str(), string2.as_str()); } println!("The longest string is '{}'", result); diff --git a/exercises/lifetimes/lifetimes3.rs b/exercises/lifetimes/lifetimes3.rs index ea48370..e28c59e 100644 --- a/exercises/lifetimes/lifetimes3.rs +++ b/exercises/lifetimes/lifetimes3.rs @@ -4,11 +4,10 @@ // // 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/quiz3.rs b/exercises/quiz3.rs index 15dc469..759fa5d 100644 --- a/exercises/quiz3.rs +++ b/exercises/quiz3.rs @@ -14,15 +14,14 @@ // Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE -pub struct ReportCard { - pub grade: f32, +pub struct ReportCard { + pub grade: T, pub student_name: String, pub student_age: u8, } -impl ReportCard { +impl ReportCard { pub fn print(&self) -> String { format!("{} ({}) - achieved a grade of {}", &self.student_name, &self.student_age, &self.grade) @@ -50,7 +49,7 @@ mod tests { fn generate_alphabetic_report_card() { // TODO: Make sure to change the grade here after you finish the exercise. let report_card = ReportCard { - grade: 2.1, + grade: "A+".to_string(), student_name: "Gary Plotter".to_string(), student_age: 11, }; diff --git a/exercises/tests/tests1.rs b/exercises/tests/tests1.rs index 8b6ea37..34baba7 100644 --- a/exercises/tests/tests1.rs +++ b/exercises/tests/tests1.rs @@ -7,12 +7,11 @@ // pass! Make the test fail! // Execute `rustlings hint tests1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE #[cfg(test)] mod tests { #[test] fn you_can_assert() { - assert!(); + assert!(true); } } diff --git a/exercises/tests/tests2.rs b/exercises/tests/tests2.rs index a5ac15b..da0e936 100644 --- a/exercises/tests/tests2.rs +++ b/exercises/tests/tests2.rs @@ -3,12 +3,11 @@ // 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!(1, 1); } } diff --git a/exercises/tests/tests3.rs b/exercises/tests/tests3.rs index 196a81a..17f6e32 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(2)); } #[test] fn is_false_when_odd() { - assert!(); + assert!(!is_even(3)); } } diff --git a/exercises/traits/traits3.rs b/exercises/traits/traits3.rs index 6d2fd6c..86e425f 100644 --- a/exercises/traits/traits3.rs +++ b/exercises/traits/traits3.rs @@ -7,10 +7,10 @@ // Consider what you can add to the Licensed trait. // Execute `rustlings hint traits3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - pub trait Licensed { - fn licensing_info(&self) -> String; + fn licensing_info(&self) -> String { + String::from("Some information") + } } struct SomeSoftware { diff --git a/exercises/traits/traits4.rs b/exercises/traits/traits4.rs index 6b54166..ab2d644 100644 --- a/exercises/traits/traits4.rs +++ b/exercises/traits/traits4.rs @@ -4,8 +4,6 @@ // Don't change any line other than the marked one. // Execute `rustlings hint traits4` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - pub trait Licensed { fn licensing_info(&self) -> String { "some information".to_string() @@ -20,7 +18,7 @@ impl Licensed for SomeSoftware {} impl Licensed for OtherSoftware {} // YOU MAY ONLY CHANGE THE NEXT LINE -fn compare_license_types(software: ??, software_two: ??) -> bool { +fn compare_license_types(software: impl Licensed, software_two: impl Licensed) -> bool { software.licensing_info() == software_two.licensing_info() } diff --git a/exercises/traits/traits5.rs b/exercises/traits/traits5.rs index 0fbca28..9c765c2 100644 --- a/exercises/traits/traits5.rs +++ b/exercises/traits/traits5.rs @@ -4,7 +4,6 @@ // Don't change any line other than the marked one. // Execute `rustlings hint traits5` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE pub trait SomeTrait { fn some_function(&self) -> bool { @@ -27,7 +26,7 @@ impl SomeTrait for OtherStruct {} impl OtherTrait for OtherStruct {} // YOU MAY ONLY CHANGE THE NEXT LINE -fn some_func(item: ??) -> bool { +fn some_func(item: (impl SomeTrait + OtherTrait)) -> bool { item.some_function() && item.other_function() }