Variables and mutability in Rust
Bindings are immutable by default; mut opts in.
fn main() {
let name = "typestar";
let count: i32 = 42;
let ratio = 3.5_f64;
let mut total = 0;
total += count;
total *= 2;
let doubled = ratio * 2.0;
println!("{name}: {total} ({doubled})");
}
How it works
letbinds a value; a type can be annotated or inferred.mutis required before a binding can change.+=and*=update a mutable binding.
Keywords and builtins used here
f64fni32letmainmut
The run, in numbers
- Lines
- 12
- Characters to type
- 195
- Tokens
- 56
- Three-star pace
- 80 tpm
At the three-star pace of 80 tokens a minute, this run takes about 42 seconds.
Step 1 of 6 in Variables & types, step 1 of 39 in Language basics.