Search⌘ K

Almost Correct: Assignment

Explore how Rust handles variable assignment with a focus on mutability. Learn why variables are immutable by default and how to modify declarations to enable reassignment. Understand this key concept to write clearer, more manageable Rust code and avoid common errors with variable assignment.

We'll cover the following...

A let statement defines a new variable. What if we did the same let keep_going = false;, but without the let? In other words, what if our program looks like this:

Rust 1.40.0
fn main() {
let keep_going = true;
while keep_going {
println!("Inside the while loop");
keep_going = false;
}
}

This is close, but no cigar. Rust complains with the following error message:

error[E0384]:
...