typestar

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

  1. A new let shadows the old binding, even changing its type.
  2. Shadowing transforms a value through several steps.
  3. const names a value fixed at compile time.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 6 in Variables & types, step 2 of 39 in Language basics.

← Previous Next →