Shadowing and constants in Rust
Reusing a name with let, and compile-time constants.
fn main() {
let spaces = " ";
let spaces = spaces.len();
let value = 5;
let value = value + 1;
let value = value * 2;
const MAX_POINTS: u32 = 100_000;
let remaining = MAX_POINTS - value as u32;
println!("{spaces} {value} {remaining}");
}
How it works
- A new
letshadows the old binding, even changing its type. - Shadowing transforms a value through several steps.
constnames a value fixed at compile time.
Keywords and builtins used here
asconstfnletmainu32
The run, in numbers
- Lines
- 12
- Characters to type
- 241
- Tokens
- 63
- Three-star pace
- 80 tpm
At the three-star pace of 80 tokens a minute, this run takes about 47 seconds.
Step 2 of 6 in Variables & types, step 2 of 39 in Language basics.