Course lesson
Rustlings variables3: Using the mut keyword to declare a mutable variable
README for this exercise
- Duration
- 1 min
- Access
- Free
- Transcript
- Available
README for this exercise
Course lesson
README for this exercise
README for this exercise
Chris Biscardi: For variables3, we have our main function again with an x that equals three, a println macro that prints the number, a reassignment of x to five, and another println macro that prints the number again.
Here in our error, we can see that we cannot assign twice to an immutable value. The error points to x and says the first assignment to x help make this binding mutable, mut x. Then, follows up with the error that we saw above cannot assign twice to immutable value.
We're doing an assignment here and an assignment here. Because Rust is immutable by default, we can't assign twice to a single value because that would mutate the value. As seen in the error message, we can use the mut keyword to set the x variable to immutable value. This lets us assign to x a second time.
Course lesson
README for this exercise