Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ members = [
"helpers/ticket_fields",
]
resolver = "2"
[profile.dev]
opt-level = 1 # Use slightly better optimizations.
overflow-checks = false # Disable integer overflow checks.

# This is needed to guarantee the expected behaviour on that specific exercise,
# regardless of the "global" setting for `overflow-checks` on the `dev` profile.
Expand Down
2 changes: 1 addition & 1 deletion exercises/01_intro/00_welcome/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions exercises/01_intro/01_syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -16,4 +16,4 @@ mod tests {
fn case() {
assert_eq!(compute(1, 2), 5);
}
}
}
2 changes: 1 addition & 1 deletion exercises/02_basic_calculator/00_intro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion exercises/02_basic_calculator/01_integers/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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.
let multiplier: u8 = 4;
let multiplier: u32 = 4;
a + b * multiplier
}

Expand Down
2 changes: 1 addition & 1 deletion exercises/02_basic_calculator/02_variables/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +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
}
Expand Down
8 changes: 7 additions & 1 deletion exercises/02_basic_calculator/03_if_else/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
/// `13` if `n` is divisible by `3`,
/// `17` otherwise.
fn magic_number(n: u32) -> u32 {
todo!()
if n % 2 == 0 {
12
} else if n % 3 == 0 {
13
} else {
17
}
}

#[cfg(test)]
Expand Down
4 changes: 3 additions & 1 deletion exercises/02_basic_calculator/04_panics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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
}

Expand Down
10 changes: 10 additions & 0 deletions exercises/02_basic_calculator/05_factorial/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
//
// 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 if n == 1 {
1
} else {
n * factorial(n - 1)
}
}

#[cfg(test)]
mod tests {
use crate::factorial;
Expand Down
14 changes: 13 additions & 1 deletion exercises/02_basic_calculator/06_while/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@ pub fn factorial(n: u32) -> u32 {
// interprets as "I'll get back to this later", thus
// suppressing type errors.
// It panics at runtime.
todo!()
if n == 0 {
1
} else if n == 1 {
1
} else {
let mut i = 0;
let mut result = 1;
while i < n {
result *= n - i;
i += 1;
}
result
}
}

#[cfg(test)]
Expand Down
12 changes: 11 additions & 1 deletion exercises/02_basic_calculator/07_for/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
// Rewrite the factorial function using a `for` loop.
pub fn factorial(n: u32) -> u32 {
todo!()
if n == 0 {
1
} else if n == 1 {
1
} else {
let mut result = 1;
for i in 1..=n {
result *= i;
}
result
}
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions exercises/02_basic_calculator/09_saturating/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub fn factorial(n: u32) -> u32 {
let mut result = 1;
let mut result = 1u32;
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
}
Expand Down
6 changes: 3 additions & 3 deletions exercises/02_basic_calculator/10_as_casting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -24,14 +24,14 @@ 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 to `u8`.
let y: i8 = todo!();
let y: i8 = -1;

assert_eq!(x, y);
}

#[test]
fn bool_to_u8() {
let v: u8 = todo!();
let v: u8 = 1;
assert_eq!(true as u8, v);
}
}