Skip to content

Commit afbb531

Browse files
committed
complete first section
1 parent 18f7e66 commit afbb531

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

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/02_basic_calculator/07_for/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
// Rewrite the factorial function using a `for` loop.
22
pub fn factorial(n: u32) -> u32 {
3-
todo!()
3+
let mut result = 1;
4+
5+
for i in 1..=n {
6+
result *= i;
7+
}
8+
result
49
}
510

611
#[cfg(test)]

exercises/02_basic_calculator/09_saturating/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
pub fn factorial(n: u32) -> u32 {
2-
let mut result = 1;
2+
let mut result: u32 = 1;
33
for i in 1..=n {
44
// Use saturating multiplication to stop at the maximum value of u32
55
// rather than overflowing and wrapping around
6-
result *= i;
6+
result = result.saturating_mul(i);
77
}
88
result
99
}

exercises/02_basic_calculator/10_as_casting/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod tests {
66

77
#[test]
88
fn u16_to_u32() {
9-
let v: u32 = todo!();
9+
let v: u32 = 47u16 as u32;
1010
assert_eq!(47u16 as u32, v);
1111
}
1212

@@ -24,14 +24,14 @@ mod tests {
2424
// You could solve this by using exactly the same expression as above,
2525
// but that would defeat the purpose of the exercise. Instead, use a genuine
2626
// `i8` value that is equivalent to `255` when converted from `u8`.
27-
let y: i8 = todo!();
27+
let y: i8 = -1;
2828

2929
assert_eq!(x, y);
3030
}
3131

3232
#[test]
3333
fn bool_to_u8() {
34-
let v: u8 = todo!();
34+
let v: u8 = 1;
3535
assert_eq!(true as u8, v);
3636
}
3737
}

0 commit comments

Comments
 (0)