Skip to content

Commit 97dffa6

Browse files
Solutions
1 parent 3f60de3 commit 97dffa6

File tree

106 files changed

+1018
-197
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+1018
-197
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ resolver = "2"
1212
# regardless of the "global" setting for `overflow-checks` on the `dev` profile.
1313
[profile.dev.package.copy]
1414
overflow-checks = true
15+
16+
[profile.dev]
17+
overflow-checks = false

exercises/01_intro/00_welcome/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
// You can also find solutions to all exercises in the `solutions` git branch.
1818
fn greeting() -> &'static str {
1919
// TODO: fix me 👇
20-
"I'm ready to __!"
20+
"I'm ready to learn Rust!"
2121
}
2222

2323
// Your solutions will be automatically verified by a set of tests.

exercises/01_intro/01_syntax/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// partner in this course and it'll often guide you in the right direction!
44
//
55
// The input parameters should have the same type of the return type.
6-
fn compute(a, b) -> u32 {
6+
fn compute(a: u32, b: u32) -> u32 {
77
// Don't touch the function body.
88
a + b * 2
99
}
@@ -16,4 +16,4 @@ mod tests {
1616
fn case() {
1717
assert_eq!(compute(1, 2), 5);
1818
}
19-
}
19+
}

exercises/02_basic_calculator/00_intro/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fn intro() -> &'static str {
22
// TODO: fix me 👇
3-
"I'm ready to __!"
3+
"I'm ready to build a calculator in Rust!"
44
}
55

66
#[cfg(test)]

exercises/02_basic_calculator/01_integers/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
fn compute(a: u32, b: u32) -> u32 {
2-
// TODO: change the line below to fix the compiler error and make the tests pass.
3-
let multiplier: u8 = 4;
2+
let multiplier: u32 = 4;
3+
// ^ You could also omit `: u32` entirely.
4+
// The compiler will infer the correct type based on the usage below.
45
a + b * multiplier
56
}
67

exercises/02_basic_calculator/02_variables/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
pub fn speed(start: u32, end: u32, time_elapsed: u32) -> u32 {
99
// TODO: define a variable named `distance` with the right value to get tests to pass
1010
// Do you need to annotate the type of `distance`? Why or why not?
11+
let distance = end - start;
1112

1213
// Don't change the line below
1314
distance / time_elapsed

exercises/02_basic_calculator/03_if_else/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
/// `13` if `n` is divisible by `3`,
33
/// `17` otherwise.
44
fn magic_number(n: u32) -> u32 {
5-
todo!()
5+
if n % 2 == 0 {
6+
12
7+
} else if n % 3 == 0 {
8+
13
9+
} else {
10+
17
11+
}
612
}
713

814
#[cfg(test)]

exercises/02_basic_calculator/04_panics/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
/// calculate the average speed of the journey.
33
fn speed(start: u32, end: u32, time_elapsed: u32) -> u32 {
44
// TODO: Panic with a custom message if `time_elapsed` is 0
5+
if time_elapsed == 0 {
6+
panic!("The journey took no time at all, that's impossible!");
7+
}
58

69
(end - start) / time_elapsed
710
}

exercises/02_basic_calculator/05_factorial/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
// `factorial(2)` to return `2`, and so on.
1010
//
1111
// Use only what you learned! No loops yet, so you'll have to use recursion!
12+
fn factorial(n: u32) -> u32 {
13+
if n == 0 {
14+
1
15+
} else {
16+
n * factorial(n - 1)
17+
}
18+
}
1219

1320
#[cfg(test)]
1421
mod tests {

0 commit comments

Comments
 (0)