diff --git a/exercises/01_intro/00_welcome/src/lib.rs b/exercises/01_intro/00_welcome/src/lib.rs index ec95b6c..e0f1fc2 100644 --- a/exercises/01_intro/00_welcome/src/lib.rs +++ b/exercises/01_intro/00_welcome/src/lib.rs @@ -17,7 +17,7 @@ // You can also find solutions to all exercises in the `solutions` git branch. fn greeting() -> &'static str { // TODO: fix me 👇 - "I'm ready to __!" + "I'm ready to learn Rust!" } // Your solutions will be automatically verified by a set of tests. diff --git a/exercises/01_intro/01_syntax/src/lib.rs b/exercises/01_intro/01_syntax/src/lib.rs index 676d81b..0f8447e 100644 --- a/exercises/01_intro/01_syntax/src/lib.rs +++ b/exercises/01_intro/01_syntax/src/lib.rs @@ -3,7 +3,7 @@ // partner in this course and it'll often guide you in the right direction! // // The input parameters should have the same type of the return type. -fn compute(a, b) -> u32 { +fn compute(a: u32, b: u32) -> u32 { // Don't touch the function body. a + b * 2 } @@ -16,4 +16,5 @@ mod tests { fn case() { assert_eq!(compute(1, 2), 5); } -} \ No newline at end of file +} + diff --git a/exercises/02_basic_calculator/00_intro/src/lib.rs b/exercises/02_basic_calculator/00_intro/src/lib.rs index 03aeb16..d65a5cd 100644 --- a/exercises/02_basic_calculator/00_intro/src/lib.rs +++ b/exercises/02_basic_calculator/00_intro/src/lib.rs @@ -1,6 +1,6 @@ fn intro() -> &'static str { // TODO: fix me 👇 - "I'm ready to __!" + "I'm ready to build a calculator in Rust!" } #[cfg(test)] diff --git a/exercises/02_basic_calculator/01_integers/src/lib.rs b/exercises/02_basic_calculator/01_integers/src/lib.rs index 555db12..e83d87f 100644 --- a/exercises/02_basic_calculator/01_integers/src/lib.rs +++ b/exercises/02_basic_calculator/01_integers/src/lib.rs @@ -1,6 +1,6 @@ fn compute(a: u32, b: u32) -> u32 { // TODO: change the line below to fix the compiler error and make the tests pass. - a + b * 4u8 + a + b * 4 } #[cfg(test)] diff --git a/exercises/02_basic_calculator/02_variables/src/lib.rs b/exercises/02_basic_calculator/02_variables/src/lib.rs index e8d1167..e5a63b2 100644 --- a/exercises/02_basic_calculator/02_variables/src/lib.rs +++ b/exercises/02_basic_calculator/02_variables/src/lib.rs @@ -8,6 +8,7 @@ pub fn speed(start: u32, end: u32, time_elapsed: u32) -> u32 { // TODO: define a variable named `distance` with the right value to get tests to pass // Do you need to annotate the type of `distance`? Why or why not? + let distance = end - start; // Don't change the line below distance / time_elapsed diff --git a/exercises/02_basic_calculator/03_if_else/src/lib.rs b/exercises/02_basic_calculator/03_if_else/src/lib.rs index 15af33e..0f47aee 100644 --- a/exercises/02_basic_calculator/03_if_else/src/lib.rs +++ b/exercises/02_basic_calculator/03_if_else/src/lib.rs @@ -1,6 +1,6 @@ /// Return `true` if `n` is even, `false` otherwise. fn is_even(n: u32) -> bool { - todo!() + n % 2 == 0 } #[cfg(test)] diff --git a/exercises/02_basic_calculator/04_panics/src/lib.rs b/exercises/02_basic_calculator/04_panics/src/lib.rs index 341a5ef..523366a 100644 --- a/exercises/02_basic_calculator/04_panics/src/lib.rs +++ b/exercises/02_basic_calculator/04_panics/src/lib.rs @@ -2,6 +2,9 @@ /// calculate the average speed of the journey. fn speed(start: u32, end: u32, time_elapsed: u32) -> u32 { // TODO: Panic with a custom message if `time_elapsed` is 0 + if time_elapsed == 0 { + panic!("The journey took no time at all, that's impossible!") + } (end - start) / time_elapsed } diff --git a/exercises/02_basic_calculator/05_factorial/src/lib.rs b/exercises/02_basic_calculator/05_factorial/src/lib.rs index d2f11a7..a846578 100644 --- a/exercises/02_basic_calculator/05_factorial/src/lib.rs +++ b/exercises/02_basic_calculator/05_factorial/src/lib.rs @@ -10,6 +10,14 @@ // // Use only what you learned! No loops yet, so you'll have to use recursion! +fn factorial(n: u32) -> u32 { + if n == 0 { + 1 + } else { + n * factorial(n - 1) + } +} + #[cfg(test)] mod tests { use crate::factorial; diff --git a/exercises/02_basic_calculator/06_while/src/lib.rs b/exercises/02_basic_calculator/06_while/src/lib.rs index dbc30eb..7d5dd36 100644 --- a/exercises/02_basic_calculator/06_while/src/lib.rs +++ b/exercises/02_basic_calculator/06_while/src/lib.rs @@ -1,10 +1,15 @@ // Rewrite the factorial function using a `while` loop. -pub fn factorial(n: u32) -> u32 { +pub fn factorial(mut n: u32) -> u32 { // The `todo!()` macro is a placeholder that the compiler // interprets as "I'll get back to this later", thus // suppressing type errors. // It panics at runtime. - todo!() + let mut out = 1; + while n > 1 { + out *= n; + n -= 1; + } + out } #[cfg(test)] diff --git a/exercises/02_basic_calculator/07_for/src/lib.rs b/exercises/02_basic_calculator/07_for/src/lib.rs index d571d57..a0e2dec 100644 --- a/exercises/02_basic_calculator/07_for/src/lib.rs +++ b/exercises/02_basic_calculator/07_for/src/lib.rs @@ -1,6 +1,10 @@ // Rewrite the factorial function using a `for` loop. pub fn factorial(n: u32) -> u32 { - todo!() + let mut out = 1; + for i in 1..=n { + out = out * i; + } + out } #[cfg(test)] diff --git a/exercises/02_basic_calculator/08_overflow/src/lib.rs b/exercises/02_basic_calculator/08_overflow/src/lib.rs index f99dd7c..68e61ea 100644 --- a/exercises/02_basic_calculator/08_overflow/src/lib.rs +++ b/exercises/02_basic_calculator/08_overflow/src/lib.rs @@ -6,9 +6,9 @@ // at the root of the repository, not in the `Cargo.toml` of the exercise. pub fn factorial(n: u32) -> u32 { - let mut result = 1; + let mut result = 1u32; for i in 1..=n { - result *= i; + result = result.wrapping_mul(i); } result } diff --git a/exercises/02_basic_calculator/09_saturating/src/lib.rs b/exercises/02_basic_calculator/09_saturating/src/lib.rs index 4b0adde..a254b43 100644 --- a/exercises/02_basic_calculator/09_saturating/src/lib.rs +++ b/exercises/02_basic_calculator/09_saturating/src/lib.rs @@ -1,9 +1,9 @@ pub fn factorial(n: u32) -> u32 { - let mut result = 1; + let mut result: u32 = 1; for i in 1..=n { // Use saturating multiplication to stop at the maximum value of u32 // rather than overflowing and wrapping around - result *= i; + result = result.saturating_mul(i); } result } diff --git a/exercises/02_basic_calculator/10_as_casting/src/lib.rs b/exercises/02_basic_calculator/10_as_casting/src/lib.rs index b1921d0..7b23971 100644 --- a/exercises/02_basic_calculator/10_as_casting/src/lib.rs +++ b/exercises/02_basic_calculator/10_as_casting/src/lib.rs @@ -6,7 +6,7 @@ mod tests { #[test] fn u16_to_u32() { - let v: u32 = todo!(); + let v: u32 = 47; assert_eq!(47u16 as u32, v); } @@ -24,14 +24,15 @@ mod tests { // You could solve this by using exactly the same expression as above, // but that would defeat the purpose of the exercise. Instead, use a genuine // `i8` value that is equivalent to `255` when converted from `u8`. - let y: i8 = todo!(); + #[allow(overflowing_literals)] + let y: i8 = 255 as _; assert_eq!(x, y); } #[test] fn bool_to_u8() { - let v: u8 = todo!(); + let v: u8 = { true as u8 }; assert_eq!(true as u8, v); } } diff --git a/main b/main new file mode 160000 index 0000000..c437f75 --- /dev/null +++ b/main @@ -0,0 +1 @@ +Subproject commit c437f75fcf768c30c1bd450b020eb6707975a0dd