1. 4
    Rustlings variables3: Using the mut keyword to declare a mutable variable
    46s

Rustlings variables3: Using the mut keyword to declare a mutable variable

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

README for this exercise

Chris Biscardi: [0:00] 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.

[0:11] 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.

[0:26] 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.