Back to questions
JuniorTheoryCommon
What happens when this program is built and run?
fn main() {
let x = 5;
x = 6;
println!("{x}");
}It does not compile
- Expecting
6, as in most languages: a Rust variable can be given a new value only when it is declared withlet mut. - Expecting the error to show up at run time: immutability is a compile-time rule, so the program is never built.
- What is the smallest change that makes the program print
6? - How could you give
xa new value without declaring itmut?