Mutability - Rust By Example 09.10.
2023 12:57
Mutability
Variable bindings are immutable by default, but this can be overridden using the mut
modifier.
1 fn main() {
2 let _immutable_binding = 1;
3 let mut mutable_binding = 1;
4
5 println!("Before mutation: {}", mutable_binding);
6
7 // Ok
8 mutable_binding += 1;
9
10 println!("After mutation: {}", mutable_binding);
11
12 // Error! Cannot assign a new value to an immutable variable
13 _immutable_binding += 1;
14 }
The compiler will throw a detailed diagnostic about mutability errors.
https://doc.rust-lang.org/rust-by-example/variable_bindings/mut.html Stránka 1 z 1