feat: complete 02

This commit is contained in:
yangrui.0 2024-10-16 19:41:19 +08:00
parent c437f75fcf
commit 800cd6d542
14 changed files with 40 additions and 16 deletions

View File

@ -17,7 +17,7 @@
// You can also find solutions to all exercises in the `solutions` git branch. // You can also find solutions to all exercises in the `solutions` git branch.
fn greeting() -> &'static str { fn greeting() -> &'static str {
// TODO: fix me 👇 // TODO: fix me 👇
"I'm ready to __!" "I'm ready to learn Rust!"
} }
// Your solutions will be automatically verified by a set of tests. // Your solutions will be automatically verified by a set of tests.

View File

@ -3,7 +3,7 @@
// partner in this course and it'll often guide you in the right direction! // 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. // 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. // Don't touch the function body.
a + b * 2 a + b * 2
} }
@ -16,4 +16,5 @@ mod tests {
fn case() { fn case() {
assert_eq!(compute(1, 2), 5); assert_eq!(compute(1, 2), 5);
} }
} }

View File

@ -1,6 +1,6 @@
fn intro() -> &'static str { fn intro() -> &'static str {
// TODO: fix me 👇 // TODO: fix me 👇
"I'm ready to __!" "I'm ready to build a calculator in Rust!"
} }
#[cfg(test)] #[cfg(test)]

View File

@ -1,6 +1,6 @@
fn compute(a: u32, b: u32) -> u32 { fn compute(a: u32, b: u32) -> u32 {
// TODO: change the line below to fix the compiler error and make the tests pass. // TODO: change the line below to fix the compiler error and make the tests pass.
a + b * 4u8 a + b * 4
} }
#[cfg(test)] #[cfg(test)]

View File

@ -8,6 +8,7 @@
pub fn speed(start: u32, end: u32, time_elapsed: u32) -> u32 { 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 // 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? // Do you need to annotate the type of `distance`? Why or why not?
let distance = end - start;
// Don't change the line below // Don't change the line below
distance / time_elapsed distance / time_elapsed

View File

@ -1,6 +1,6 @@
/// Return `true` if `n` is even, `false` otherwise. /// Return `true` if `n` is even, `false` otherwise.
fn is_even(n: u32) -> bool { fn is_even(n: u32) -> bool {
todo!() n % 2 == 0
} }
#[cfg(test)] #[cfg(test)]

View File

@ -2,6 +2,9 @@
/// calculate the average speed of the journey. /// calculate the average speed of the journey.
fn speed(start: u32, end: u32, time_elapsed: u32) -> u32 { fn speed(start: u32, end: u32, time_elapsed: u32) -> u32 {
// TODO: Panic with a custom message if `time_elapsed` is 0 // 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 (end - start) / time_elapsed
} }

View File

@ -10,6 +10,14 @@
// //
// Use only what you learned! No loops yet, so you'll have to use recursion! // 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)] #[cfg(test)]
mod tests { mod tests {
use crate::factorial; use crate::factorial;

View File

@ -1,10 +1,15 @@
// Rewrite the factorial function using a `while` loop. // 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 // The `todo!()` macro is a placeholder that the compiler
// interprets as "I'll get back to this later", thus // interprets as "I'll get back to this later", thus
// suppressing type errors. // suppressing type errors.
// It panics at runtime. // It panics at runtime.
todo!() let mut out = 1;
while n > 1 {
out *= n;
n -= 1;
}
out
} }
#[cfg(test)] #[cfg(test)]

View File

@ -1,6 +1,10 @@
// Rewrite the factorial function using a `for` loop. // Rewrite the factorial function using a `for` loop.
pub fn factorial(n: u32) -> u32 { pub fn factorial(n: u32) -> u32 {
todo!() let mut out = 1;
for i in 1..=n {
out = out * i;
}
out
} }
#[cfg(test)] #[cfg(test)]

View File

@ -6,9 +6,9 @@
// at the root of the repository, not in the `Cargo.toml` of the exercise. // at the root of the repository, not in the `Cargo.toml` of the exercise.
pub fn factorial(n: u32) -> u32 { pub fn factorial(n: u32) -> u32 {
let mut result = 1; let mut result = 1u32;
for i in 1..=n { for i in 1..=n {
result *= i; result = result.wrapping_mul(i);
} }
result result
} }

View File

@ -1,9 +1,9 @@
pub fn factorial(n: u32) -> u32 { pub fn factorial(n: u32) -> u32 {
let mut result = 1; let mut result: u32 = 1;
for i in 1..=n { for i in 1..=n {
// Use saturating multiplication to stop at the maximum value of u32 // Use saturating multiplication to stop at the maximum value of u32
// rather than overflowing and wrapping around // rather than overflowing and wrapping around
result *= i; result = result.saturating_mul(i);
} }
result result
} }

View File

@ -6,7 +6,7 @@ mod tests {
#[test] #[test]
fn u16_to_u32() { fn u16_to_u32() {
let v: u32 = todo!(); let v: u32 = 47;
assert_eq!(47u16 as u32, v); assert_eq!(47u16 as u32, v);
} }
@ -24,14 +24,15 @@ mod tests {
// You could solve this by using exactly the same expression as above, // 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 // but that would defeat the purpose of the exercise. Instead, use a genuine
// `i8` value that is equivalent to `255` when converted from `u8`. // `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); assert_eq!(x, y);
} }
#[test] #[test]
fn bool_to_u8() { fn bool_to_u8() {
let v: u8 = todo!(); let v: u8 = { true as u8 };
assert_eq!(true as u8, v); assert_eq!(true as u8, v);
} }
} }

1
main Submodule

@ -0,0 +1 @@
Subproject commit c437f75fcf768c30c1bd450b020eb6707975a0dd