1. 6
    Rustlings variables5: Using block scopes to shadow previously declared variables
    1m 25s

Rustlings variables5: Using block scopes to shadow previously declared variables

InstructorChris Biscardi

Share this video with your friends

Send Tweet

README for this exercise

Stephen James
~ 7 months ago

The solution I found for this was to add let to line 9 using shadowing. This came from the page linked to in the hint.

mi-skam .
~ 6 months ago

You don't even need block scope to shadow the variables, it works in the same scope simply by using let number = "something", which blends in the new value.

Anthony Albertorio
~ 6 months ago

According to the docs we can use let again aka "shadowing" within the same scope to re-assign the variable. This removes the need for the extra curly braces and scope.

By using let, we can perform a few transformations on a value but have the variable be immutable after those transformations have been completed. The other difference between mut and shadowing is that because we’re effectively creating a new variable when we use the let keyword again, we can change the type of the value but reuse the same name.

fn main() {
    let number = "3"; // don't change this line
    println!("Number {}", number);
    let number = 3; // shadow the variable number, add let
    println!("Number {}", number);
}

Hope this helps.