Let Without Assignment
We'll cover the following...
So far, we’ve immediately assigned values to variables with let
. However, it’s valid in Rust to separate these steps out. For example:
Press + to interact
fn main() {let x;x = 5;println!("x == {}", x);let mut y;y = 5;y += 1;println!("y == {}", y);}
I point this out now because it may be surprising that:
-
x
is an immutable variable -
But we’re allowed to say
x = 5
...