Google News
logo
Rust - Interview Questions
What is the difference between a mutable and an immutable variable in Rust?
A mutable variable is one whose value can be edited after the assignment, whereas an immutable variable is one whose value can’t be edited after the assignment. For declaring a mutable variable, you can use the mut keyword:
let mut x = 5;​

Since x is mutable, its value can be changed later in the program by assigning it a new value.

For declaring an immutable variable, you can omit the mut keyword and just declare the variable as
let y = 20;​
Advertisement