Files
100-exercises-to-learn-rust/book/src/02_basic_calculator/06_while.md
Luca Palmieri 1aae615bb4 Automatically add exercise links to sections. (#52)
We use an mdbook preprocessor to automatically generate links to the relevant exercise for each section.
We remove all existing manual links and refactor the deploy process to push the rendered book to a branch.
2024-05-24 18:15:38 +02:00

2.1 KiB
Raw Blame History

Loops, part 1: while

Your implementation of factorial has been forced to use recursion.
This may feel natural to you, especially if youre coming from a functional programming background. Or it may feel strange, if youre used to more imperative languages like C or Python.

Lets see how you can implement the same functionality using a loop instead.

The while loop

A while loop is a way to execute a block of code as long as a condition is true.
Heres the general syntax:

while <condition> {
    // code to execute
}

For example, we might want to sum the numbers from 1 to 5:

let sum = 0;
let i = 1;
// "while i is less than or equal to 5"
while i <= 5 {
    // `+=` is a shorthand for `sum = sum + i`
    sum += i;
    i += 1;
}

This will keep adding 1 to i and i to sum until i is no longer less than or equal to 5.

The mut keyword

The example above wont compile as is. Youll get an error like:

error[E0384]: cannot assign twice to immutable variable `sum`
 --> src/main.rs:7:9
  |
2 |     let sum = 0;
  |         ---
  |         |
  |         first assignment to `sum`
  |         help: consider making this binding mutable: `mut sum`
...
7 |         sum += i;
  |         ^^^^^^^^ cannot assign twice to immutable variable

error[E0384]: cannot assign twice to immutable variable `i`
 --> src/main.rs:8:9
  |
3 |     let i = 1;
  |         -
  |         |
  |         first assignment to `i`
  |         help: consider making this binding mutable: `mut i`
...
8 |         i += 1;
  |         ^^^^^^ cannot assign twice to immutable variable

This is because variables in Rust are immutable by default.
You cant change their value once it has been assigned.

If you want to allow modifications, you have to declare the variable as mutable using the mut keyword:

// `sum` and `i` are mutable now!
let mut sum = 0;
let mut i = 1;

while i <= 5 {
    sum += i;
    i += 1;
}

This will compile and run without errors.

Further reading