feat: complete 02
This commit is contained in:
parent
c437f75fcf
commit
800cd6d542
|
@ -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.
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/// Return `true` if `n` is even, `false` otherwise.
|
||||
fn is_even(n: u32) -> bool {
|
||||
todo!()
|
||||
n % 2 == 0
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Subproject commit c437f75fcf768c30c1bd450b020eb6707975a0dd
|
Loading…
Reference in New Issue