]> git.proxmox.com Git - rustc.git/blob - src/doc/rust-by-example/src/variable_bindings/mut.md
New upstream version 1.70.0+dfsg1
[rustc.git] / src / doc / rust-by-example / src / variable_bindings / mut.md
1 # Mutability
2
3 Variable bindings are immutable by default, but this can be overridden using
4 the `mut` modifier.
5
6 ```rust,editable,ignore,mdbook-runnable
7 fn main() {
8 let _immutable_binding = 1;
9 let mut mutable_binding = 1;
10
11 println!("Before mutation: {}", mutable_binding);
12
13 // Ok
14 mutable_binding += 1;
15
16 println!("After mutation: {}", mutable_binding);
17
18 // Error! Cannot assign a new value to an immutable variable
19 _immutable_binding += 1;
20 }
21 ```
22
23 The compiler will throw a detailed diagnostic about mutability errors.