diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000000..13566b81b0
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/.idea/MyRustWay.iml b/.idea/MyRustWay.iml
new file mode 100644
index 0000000000..d6ebd48059
--- /dev/null
+++ b/.idea/MyRustWay.iml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000000..31e1ebce6f
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000000..c964c9d738
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000000..35eb1ddfbb
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/book/src/01_intro/01_syntax.md b/book/src/01_intro/01_syntax.md
index ae75733310..c69809a6b6 100644
--- a/book/src/01_intro/01_syntax.md
+++ b/book/src/01_intro/01_syntax.md
@@ -110,6 +110,6 @@ Every single value in Rust has a type and that type must be known to the compile
Types are a form of **static analysis**.\
You can think of a type as a **tag** that the compiler attaches to every value in your program. Depending on the
-tag, the compiler can enforce different rules—e.g. you can't add a string to a number, but you can add two numbers
+tag, the compiler can enforce different rules—e.g. you can't add a string to a number, but you can add two NUMBERS
together.
If leveraged correctly, types can prevent whole classes of runtime bugs.
diff --git a/book/src/02_basic_calculator/01_integers.md b/book/src/02_basic_calculator/01_integers.md
index 765b7b465f..767d18117e 100644
--- a/book/src/02_basic_calculator/01_integers.md
+++ b/book/src/02_basic_calculator/01_integers.md
@@ -19,8 +19,8 @@ An integer is a number that can be written without a fractional component. E.g.
### Signed vs. unsigned
An integer can be **signed** or **unsigned**.\
-An unsigned integer can only represent non-negative numbers (i.e. `0` or greater).
-A signed integer can represent both positive and negative numbers (e.g. `-1`, `12`, etc.).
+An unsigned integer can only represent non-negative NUMBERS (i.e. `0` or greater).
+A signed integer can represent both positive and negative NUMBERS (e.g. `-1`, `12`, etc.).
The `u` in `u32` stands for **unsigned**.\
The equivalent type for signed integer is `i32`, where the `i` stands for integer (i.e. any integer, positive or
@@ -29,12 +29,12 @@ negative).
### Bit width
The `32` in `u32` refers to the **number of bits[^bit]** used to represent the number in memory.\
-The more bits, the larger the range of numbers that can be represented.
+The more bits, the larger the range of NUMBERS that can be represented.
Rust supports multiple bit widths for integers: `8`, `16`, `32`, `64`, `128`.
-With 32 bits, `u32` can represent numbers from `0` to `2^32 - 1` (a.k.a. [`u32::MAX`](https://doc.rust-lang.org/std/primitive.u32.html#associatedconstant.MAX)).\
-With the same number of bits, a signed integer (`i32`) can represent numbers from `-2^31` to `2^31 - 1`
+With 32 bits, `u32` can represent NUMBERS from `0` to `2^32 - 1` (a.k.a. [`u32::MAX`](https://doc.rust-lang.org/std/primitive.u32.html#associatedconstant.MAX)).\
+With the same number of bits, a signed integer (`i32`) can represent NUMBERS from `-2^31` to `2^31 - 1`
(i.e. from [`i32::MIN`](https://doc.rust-lang.org/std/primitive.i32.html#associatedconstant.MIN)
to [`i32::MAX`](https://doc.rust-lang.org/std/primitive.i32.html#associatedconstant.MAX)).\
The maximum value for `i32` is smaller than the maximum value for `u32` because one bit is used to represent
@@ -69,7 +69,7 @@ explicitly typed as a `u64`.
### Underscores in literals
-You can use underscores `_` to improve the readability of large numbers.\
+You can use underscores `_` to improve the readability of large NUMBERS.\
For example, `1_000_000` is the same as `1000000`.
## Arithmetic operators
diff --git a/book/src/02_basic_calculator/06_while.md b/book/src/02_basic_calculator/06_while.md
index 150d8870c9..f621b98d10 100644
--- a/book/src/02_basic_calculator/06_while.md
+++ b/book/src/02_basic_calculator/06_while.md
@@ -17,7 +17,7 @@ while {
}
```
-For example, we might want to sum the numbers from 1 to 5:
+For example, we might want to sum the NUMBERS from 1 to 5:
```rust
let sum = 0;
diff --git a/book/src/02_basic_calculator/07_for.md b/book/src/02_basic_calculator/07_for.md
index 896ffe4783..b502245c8d 100644
--- a/book/src/02_basic_calculator/07_for.md
+++ b/book/src/02_basic_calculator/07_for.md
@@ -17,9 +17,9 @@ for in {
## Ranges
-Rust's standard library provides **range** type that can be used to iterate over a sequence of numbers[^weird-ranges].
+Rust's standard library provides **range** type that can be used to iterate over a sequence of NUMBERS[^weird-ranges].
-For example, if we want to sum the numbers from 1 to 5:
+For example, if we want to sum the NUMBERS from 1 to 5:
```rust
let mut sum = 0;
@@ -32,9 +32,9 @@ Every time the loop runs, `i` will be assigned the next value in the range befor
There are five kinds of ranges in Rust:
-- `1..5`: A (half-open) range. It includes all numbers from 1 to 4. It doesn't include the last value, 5.
-- `1..=5`: An inclusive range. It includes all numbers from 1 to 5. It includes the last value, 5.
-- `1..`: An open-ended range. It includes all numbers from 1 to infinity (well, until the maximum value of the integer type).
+- `1..5`: A (half-open) range. It includes all NUMBERS from 1 to 4. It doesn't include the last value, 5.
+- `1..=5`: An inclusive range. It includes all NUMBERS from 1 to 5. It includes the last value, 5.
+- `1..`: An open-ended range. It includes all NUMBERS from 1 to infinity (well, until the maximum value of the integer type).
- `..5`: A range that starts at the minimum value for the integer type and ends at 4. It doesn't include the last value, 5.
- `..=5`: A range that starts at the minimum value for the integer type and ends at 5. It includes the last value, 5.
diff --git a/book/src/02_basic_calculator/08_overflow.md b/book/src/02_basic_calculator/08_overflow.md
index 9b8c5494bd..0b3dcccc5a 100644
--- a/book/src/02_basic_calculator/08_overflow.md
+++ b/book/src/02_basic_calculator/08_overflow.md
@@ -19,7 +19,7 @@ But the _mathematically correct result_ doesn't fit into that integer type!
> The `speed` function you wrote in the ["Variables" section](02_variables.md) underflowed for some input
> combinations.
> E.g. if `end` is smaller than `start`, `end - start` will underflow the `u32` type since the result is supposed
-> to be negative but `u32` can't represent negative numbers.
+> to be negative but `u32` can't represent negative NUMBERS.
## No automatic promotion
diff --git a/book/src/06_ticket_management/01_arrays.md b/book/src/06_ticket_management/01_arrays.md
index fb72c28521..af3a38275d 100644
--- a/book/src/06_ticket_management/01_arrays.md
+++ b/book/src/06_ticket_management/01_arrays.md
@@ -15,7 +15,7 @@ Here's how you can define an array:
```rust
// Array type syntax: [ ; ]
-let numbers: [u32; 3] = [1, 2, 3];
+let NUMBERS: [u32; 3] = [1, 2, 3];
```
This creates an array of 3 integers, initialized with the values `1`, `2`, and `3`.\
@@ -25,7 +25,7 @@ If all array elements are the same, you can use a shorter syntax to initialize i
```rust
// [ ; ]
-let numbers: [u32; 3] = [1; 3];
+let NUMBERS: [u32; 3] = [1; 3];
```
`[1; 3]` creates an array of three elements, all equal to `1`.
@@ -35,9 +35,9 @@ let numbers: [u32; 3] = [1; 3];
You can access elements of an array using square brackets:
```rust
-let first = numbers[0];
-let second = numbers[1];
-let third = numbers[2];
+let first = NUMBERS[0];
+let second = NUMBERS[1];
+let third = NUMBERS[2];
```
The index must be of type `usize`.\
@@ -49,8 +49,8 @@ tuples/tuple-like variants.
If you try to access an element that's out of bounds, Rust will panic:
```rust
-let numbers: [u32; 3] = [1, 2, 3];
-let fourth = numbers[3]; // This will panic
+let NUMBERS: [u32; 3] = [1, 2, 3];
+let fourth = NUMBERS[3]; // This will panic
```
This is enforced at runtime using **bounds checking**. It comes with a small performance overhead, but it's how
@@ -61,11 +61,11 @@ more about this later on.
If you don't want to panic, you can use the `get` method, which returns an `Option<&T>`:
```rust
-let numbers: [u32; 3] = [1, 2, 3];
-assert_eq!(numbers.get(0), Some(&1));
+let NUMBERS: [u32; 3] = [1, 2, 3];
+assert_eq!(NUMBERS.get(0), Some(&1));
// You get a `None` if you try to access an out-of-bounds index
// rather than a panic.
-assert_eq!(numbers.get(3), None);
+assert_eq!(NUMBERS.get(3), None);
```
### Performance
@@ -74,7 +74,7 @@ Since the size of an array is known at compile-time, the compiler can allocate t
If you run the following code:
```rust
-let numbers: [u32; 3] = [1, 2, 3];
+let NUMBERS: [u32; 3] = [1, 2, 3];
```
You'll get the following memory layout:
diff --git a/book/src/06_ticket_management/02_vec.md b/book/src/06_ticket_management/02_vec.md
index 8040eb147b..1d3b32f6d5 100644
--- a/book/src/06_ticket_management/02_vec.md
+++ b/book/src/06_ticket_management/02_vec.md
@@ -5,7 +5,7 @@ If you try to create an array with a size that's only known at runtime, you'll g
```rust
let n = 10;
-let numbers: [u32; n];
+let NUMBERS: [u32; n];
```
```text
@@ -13,7 +13,7 @@ error[E0435]: attempt to use a non-constant value in a constant
--> src/main.rs:3:20
|
2 | let n = 10;
-3 | let numbers: [u32; n];
+3 | let NUMBERS: [u32; n];
| ^ non-constant value
```
@@ -26,22 +26,22 @@ This is where `Vec` comes in.
You can create an empty array using the `Vec::new` function:
```rust
-let mut numbers: Vec = Vec::new();
+let mut NUMBERS: Vec = Vec::new();
```
You would then push elements into the vector using the `push` method:
```rust
-numbers.push(1);
-numbers.push(2);
-numbers.push(3);
+NUMBERS.push(1);
+NUMBERS.push(2);
+NUMBERS.push(3);
```
New values are added to the end of the vector.\
You can also create an initialized vector using the `vec!` macro, if you know the values at creation time:
```rust
-let numbers = vec![1, 2, 3];
+let NUMBERS = vec![1, 2, 3];
```
## Accessing elements
@@ -49,21 +49,21 @@ let numbers = vec![1, 2, 3];
The syntax for accessing elements is the same as with arrays:
```rust
-let numbers = vec![1, 2, 3];
-let first = numbers[0];
-let second = numbers[1];
-let third = numbers[2];
+let NUMBERS = vec![1, 2, 3];
+let first = NUMBERS[0];
+let second = NUMBERS[1];
+let third = NUMBERS[2];
```
The index must be of type `usize`.\
You can also use the `get` method, which returns an `Option<&T>`:
```rust
-let numbers = vec![1, 2, 3];
-assert_eq!(numbers.get(0), Some(&1));
+let NUMBERS = vec![1, 2, 3];
+assert_eq!(NUMBERS.get(0), Some(&1));
// You get a `None` if you try to access an out-of-bounds index
// rather than a panic.
-assert_eq!(numbers.get(3), None);
+assert_eq!(NUMBERS.get(3), None);
```
Access is bounds-checked, just like element access with arrays. It has O(1) complexity.
@@ -76,9 +76,9 @@ When you create a `Vec`, it allocates memory on the heap to store the elements.
If you run the following code:
```rust
-let mut numbers = Vec::with_capacity(3);
-numbers.push(1);
-numbers.push(2);
+let mut NUMBERS = Vec::with_capacity(3);
+NUMBERS.push(1);
+NUMBERS.push(2);
```
you'll get the following memory layout:
diff --git a/book/src/06_ticket_management/03_resizing.md b/book/src/06_ticket_management/03_resizing.md
index e28cde365c..67c2950364 100644
--- a/book/src/06_ticket_management/03_resizing.md
+++ b/book/src/06_ticket_management/03_resizing.md
@@ -4,11 +4,11 @@ We said that `Vec` is a "growable" vector type, but what does that mean?
What happens if you try to insert an element into a `Vec` that's already at maximum capacity?
```rust
-let mut numbers = Vec::with_capacity(3);
-numbers.push(1);
-numbers.push(2);
-numbers.push(3); // Max capacity reached
-numbers.push(4); // What happens here?
+let mut NUMBERS = Vec::with_capacity(3);
+NUMBERS.push(1);
+NUMBERS.push(2);
+NUMBERS.push(3); // Max capacity reached
+NUMBERS.push(4); // What happens here?
```
The `Vec` will **resize** itself.\
diff --git a/book/src/06_ticket_management/05_iter.md b/book/src/06_ticket_management/05_iter.md
index 379508e784..46c5ce9fa0 100644
--- a/book/src/06_ticket_management/05_iter.md
+++ b/book/src/06_ticket_management/05_iter.md
@@ -13,9 +13,9 @@ Most collections expose a method called `.iter()` that returns an iterator over
For example:
```rust
-let numbers: Vec = vec![1, 2];
+let NUMBERS: Vec = vec![1, 2];
// `n` has type `&u32` here
-for n in numbers.iter() {
+for n in NUMBERS.iter() {
// [...]
}
```
@@ -25,11 +25,11 @@ In our example above, that would be `&Vec`.\
The standard library does this, that's why the following code works:
```rust
-let numbers: Vec = vec![1, 2];
+let NUMBERS: Vec = vec![1, 2];
// `n` has type `&u32` here
// We didn't have to call `.iter()` explicitly
-// It was enough to use `&numbers` in the `for` loop
-for n in &numbers {
+// It was enough to use `&NUMBERS` in the `for` loop
+for n in &NUMBERS {
// [...]
}
```
diff --git a/book/src/06_ticket_management/07_combinators.md b/book/src/06_ticket_management/07_combinators.md
index 0b9afe5dfc..3457d5be00 100644
--- a/book/src/06_ticket_management/07_combinators.md
+++ b/book/src/06_ticket_management/07_combinators.md
@@ -19,9 +19,9 @@ These methods are called **combinators**.\
They are usually **chained** together to create complex transformations in a concise and readable way:
```rust
-let numbers = vec![1, 2, 3, 4, 5];
-// The sum of the squares of the even numbers
-let outcome: u32 = numbers.iter()
+let NUMBERS = vec![1, 2, 3, 4, 5];
+// The sum of the squares of the even NUMBERS
+let outcome: u32 = NUMBERS.iter()
.filter(|&n| n % 2 == 0)
.map(|&n| n * n)
.sum();
@@ -76,11 +76,11 @@ You either iterate over the transformed values using a `for` loop, or you collec
The latter is done using the `collect` method.\
`collect` consumes the iterator and collects its elements into a collection of your choice.
-For example, you can collect the squares of the even numbers into a `Vec`:
+For example, you can collect the squares of the even NUMBERS into a `Vec`:
```rust
-let numbers = vec![1, 2, 3, 4, 5];
-let squares_of_evens: Vec = numbers.iter()
+let NUMBERS = vec![1, 2, 3, 4, 5];
+let squares_of_evens: Vec = NUMBERS.iter()
.filter(|&n| n % 2 == 0)
.map(|&n| n * n)
.collect();
@@ -92,7 +92,7 @@ In the example above, we annotated the type of `squares_of_evens` to be `Vec::()`
diff --git a/book/src/06_ticket_management/10_slices.md b/book/src/06_ticket_management/10_slices.md
index 3a9fae5d01..f8e4167e4e 100644
--- a/book/src/06_ticket_management/10_slices.md
+++ b/book/src/06_ticket_management/10_slices.md
@@ -3,9 +3,9 @@
Let's go back to the memory layout of a `Vec`:
```rust
-let mut numbers = Vec::with_capacity(3);
-numbers.push(1);
-numbers.push(2);
+let mut NUMBERS = Vec::with_capacity(3);
+NUMBERS.push(1);
+NUMBERS.push(2);
```
```text
@@ -32,24 +32,24 @@ It's most commonly used in its borrowed form, `&[T]`.
There are various ways to create a slice reference from a `Vec`:
```rust
-let numbers = vec![1, 2, 3];
+let NUMBERS = vec![1, 2, 3];
// Via index syntax
-let slice: &[i32] = &numbers[..];
+let slice: &[i32] = &NUMBERS[..];
// Via a method
-let slice: &[i32] = numbers.as_slice();
+let slice: &[i32] = NUMBERS.as_slice();
// Or for a subset of the elements
-let slice: &[i32] = &numbers[1..];
+let slice: &[i32] = &NUMBERS[1..];
```
`Vec` implements the `Deref` trait using `[T]` as the target type, so you can use slice methods on a `Vec` directly
thanks to deref coercion:
```rust
-let numbers = vec![1, 2, 3];
+let NUMBERS = vec![1, 2, 3];
// Surprise, surprise: `iter` is not a method on `Vec`!
// It's a method on `&[T]`, but you can call it on a `Vec`
// thanks to deref coercion.
-let sum: i32 = numbers.iter().sum();
+let sum: i32 = NUMBERS.iter().sum();
```
### Memory layout
@@ -60,19 +60,19 @@ It consists of a pointer to the first element of the slice and the length of the
If you have a `Vec` with three elements:
```rust
-let numbers = vec![1, 2, 3];
+let NUMBERS = vec![1, 2, 3];
```
and then create a slice reference:
```rust
-let slice: &[i32] = &numbers[1..];
+let slice: &[i32] = &NUMBERS[1..];
```
you'll get this memory layout:
```text
- numbers slice
+ NUMBERS slice
+---------+--------+----------+ +---------+--------+
Stack | pointer | length | capacity | | pointer | length |
| | | 3 | 4 | | | | 2 |
diff --git a/book/src/06_ticket_management/11_mutable_slices.md b/book/src/06_ticket_management/11_mutable_slices.md
index b3fda116e6..ddc89ad920 100644
--- a/book/src/06_ticket_management/11_mutable_slices.md
+++ b/book/src/06_ticket_management/11_mutable_slices.md
@@ -6,8 +6,8 @@ But slices can also be mutable!
Here's how you create a mutable slice:
```rust
-let mut numbers = vec![1, 2, 3];
-let slice: &mut [i32] = &mut numbers;
+let mut NUMBERS = vec![1, 2, 3];
+let slice: &mut [i32] = &mut NUMBERS;
```
You can then modify the elements in the slice:
@@ -27,8 +27,8 @@ That's **not** the case with mutable borrows.
Consider this scenario:
```rust
-let mut numbers = Vec::with_capacity(2);
-let mut slice: &mut [i32] = &mut numbers;
+let mut NUMBERS = Vec::with_capacity(2);
+let mut slice: &mut [i32] = &mut NUMBERS;
slice.push(1);
```
diff --git a/book/src/06_ticket_management/15_hashmap.md b/book/src/06_ticket_management/15_hashmap.md
index 2d716c30e5..c7dde74240 100644
--- a/book/src/06_ticket_management/15_hashmap.md
+++ b/book/src/06_ticket_management/15_hashmap.md
@@ -83,7 +83,7 @@ when dealing with hash collisions—i.e. when two different keys hash to the sam
You may wonder: isn't that what the `PartialEq` trait is for? Almost!\
`PartialEq` is not enough for `HashMap` because it doesn't guarantee reflexivity, i.e. `a == a` is always `true`.\
-For example, floating point numbers (`f32` and `f64`) implement `PartialEq`,
+For example, floating point NUMBERS (`f32` and `f64`) implement `PartialEq`,
but they don't satisfy the reflexivity property: `f32::NAN == f32::NAN` is `false`.\
Reflexivity is crucial for `HashMap` to work correctly: without it, you wouldn't be able to retrieve a value
from the map using the same key you used to insert it.
diff --git a/book/src/07_threads/11_locks.md b/book/src/07_threads/11_locks.md
index e0360d0e2c..147b51e720 100644
--- a/book/src/07_threads/11_locks.md
+++ b/book/src/07_threads/11_locks.md
@@ -4,7 +4,7 @@ The patching strategy you just implemented has a major drawback: it's racy.\
If two clients send patches for the same ticket roughly at same time, the server will apply them in an arbitrary order.
Whoever enqueues their patch last will overwrite the changes made by the other client.
-## Version numbers
+## Version NUMBERS
We could try to fix this by using a **version number**.\
Each ticket gets assigned a version number upon creation, set to `0`.\
diff --git a/exercises/01_intro/00_welcome/src/lib.rs b/exercises/01_intro/00_welcome/src/lib.rs
index ec95b6ca75..e0f1fc2653 100644
--- a/exercises/01_intro/00_welcome/src/lib.rs
+++ b/exercises/01_intro/00_welcome/src/lib.rs
@@ -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.
diff --git a/exercises/01_intro/01_syntax/src/lib.rs b/exercises/01_intro/01_syntax/src/lib.rs
index 676d81b22e..001d56ab52 100644
--- a/exercises/01_intro/01_syntax/src/lib.rs
+++ b/exercises/01_intro/01_syntax/src/lib.rs
@@ -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
}
diff --git a/exercises/02_basic_calculator/00_intro/src/lib.rs b/exercises/02_basic_calculator/00_intro/src/lib.rs
index 03aeb16339..d65a5cd207 100644
--- a/exercises/02_basic_calculator/00_intro/src/lib.rs
+++ b/exercises/02_basic_calculator/00_intro/src/lib.rs
@@ -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)]
diff --git a/exercises/02_basic_calculator/01_integers/src/lib.rs b/exercises/02_basic_calculator/01_integers/src/lib.rs
index a87b56fba3..4eb97f56ee 100644
--- a/exercises/02_basic_calculator/01_integers/src/lib.rs
+++ b/exercises/02_basic_calculator/01_integers/src/lib.rs
@@ -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
}
diff --git a/exercises/02_basic_calculator/02_variables/src/lib.rs b/exercises/02_basic_calculator/02_variables/src/lib.rs
index e8d116749a..a4b8a438cb 100644
--- a/exercises/02_basic_calculator/02_variables/src/lib.rs
+++ b/exercises/02_basic_calculator/02_variables/src/lib.rs
@@ -7,8 +7,8 @@
/// calculate the average speed.
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?
-
+ // 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
}
diff --git a/exercises/02_basic_calculator/03_if_else/src/lib.rs b/exercises/02_basic_calculator/03_if_else/src/lib.rs
index fa2e06ea76..1c478abda2 100644
--- a/exercises/02_basic_calculator/03_if_else/src/lib.rs
+++ b/exercises/02_basic_calculator/03_if_else/src/lib.rs
@@ -2,7 +2,11 @@
/// `13` if `n` is divisible by `3`,
/// `17` otherwise.
fn magic_number(n: u32) -> u32 {
- todo!()
+ let ans =
+ if n%2 == 0{12}
+ else if n%3 == 0{13}
+ else {17};
+ ans
}
#[cfg(test)]
diff --git a/exercises/02_basic_calculator/04_panics/src/lib.rs b/exercises/02_basic_calculator/04_panics/src/lib.rs
index 702b7bd85c..edfc25cd3b 100644
--- a/exercises/02_basic_calculator/04_panics/src/lib.rs
+++ b/exercises/02_basic_calculator/04_panics/src/lib.rs
@@ -2,7 +2,7 @@
/// 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
}
diff --git a/exercises/02_basic_calculator/05_factorial/src/lib.rs b/exercises/02_basic_calculator/05_factorial/src/lib.rs
index d2f11a7216..5e894f4fe7 100644
--- a/exercises/02_basic_calculator/05_factorial/src/lib.rs
+++ b/exercises/02_basic_calculator/05_factorial/src/lib.rs
@@ -9,6 +9,16 @@
// `factorial(2)` to return `2`, and so on.
//
// Use only what you learned! No loops yet, so you'll have to use recursion!
+static mut NUMBERS: [i32 ; 7] = [-1;7];
+
+fn factorial(n : i32) -> i32{
+ if n == 1{return 1;}
+ let new_idx = n as usize;
+ unsafe {
+ NUMBERS[new_idx] = factorial(n - 1) * n;
+ NUMBERS[new_idx]
+ }
+}
#[cfg(test)]
mod tests {
diff --git a/exercises/02_basic_calculator/06_while/src/lib.rs b/exercises/02_basic_calculator/06_while/src/lib.rs
index dbc30ebb2d..fd74abdc84 100644
--- a/exercises/02_basic_calculator/06_while/src/lib.rs
+++ b/exercises/02_basic_calculator/06_while/src/lib.rs
@@ -4,7 +4,14 @@ pub fn factorial(n: u32) -> u32 {
// interprets as "I'll get back to this later", thus
// suppressing type errors.
// It panics at runtime.
- todo!()
+ todo!();
+ if n == 0{return 0;}
+ let ans = 1 as u32;
+ while n >= 1{
+ ans *= n;
+ n -= 1;
+ }
+ ans
}
#[cfg(test)]
diff --git a/exercises/02_basic_calculator/07_for/src/lib.rs b/exercises/02_basic_calculator/07_for/src/lib.rs
index d571d57d90..5a28c8c7ab 100644
--- a/exercises/02_basic_calculator/07_for/src/lib.rs
+++ b/exercises/02_basic_calculator/07_for/src/lib.rs
@@ -1,6 +1,9 @@
// Rewrite the factorial function using a `for` loop.
pub fn factorial(n: u32) -> u32 {
- todo!()
+ todo!();
+ let ans = 1 as u32;
+ for i in 1..=n {ans *= i; }
+ ans
}
#[cfg(test)]
diff --git a/exercises/02_basic_calculator/08_overflow/Cargo.toml b/exercises/02_basic_calculator/08_overflow/Cargo.toml
index b5bd80ee35..e0c549efb5 100644
--- a/exercises/02_basic_calculator/08_overflow/Cargo.toml
+++ b/exercises/02_basic_calculator/08_overflow/Cargo.toml
@@ -3,6 +3,10 @@ name = "overflow"
version = "0.1.0"
edition = "2021"
+[profile.dev]
+overflow-checks = false # Disable integer overflow checks.
+
+
[lints.rust]
# We silence dead code warnings for the time being in order to reduce
# compiler noise.
diff --git a/exercises/02_basic_calculator/09_saturating/src/lib.rs b/exercises/02_basic_calculator/09_saturating/src/lib.rs
index 4b0addec59..e6b561147f 100644
--- a/exercises/02_basic_calculator/09_saturating/src/lib.rs
+++ b/exercises/02_basic_calculator/09_saturating/src/lib.rs
@@ -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
}
diff --git a/exercises/02_basic_calculator/10_as_casting/src/lib.rs b/exercises/02_basic_calculator/10_as_casting/src/lib.rs
index 2ba058c49a..6a783235dc 100644
--- a/exercises/02_basic_calculator/10_as_casting/src/lib.rs
+++ b/exercises/02_basic_calculator/10_as_casting/src/lib.rs
@@ -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,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);
}
}