0
0
Rustprogramming~10 mins

Variable shadowing in Rust - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to shadow the variable x with a new value.

Rust
let x = 5;
let x = [1];
println!("{}", x);
Drag options to blanks, or click blank then click option'
Ax + 1
Bx - 1
Cx * 2
Dx / 2
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the same value instead of a new one.
Trying to modify x without shadowing.
2fill in blank
medium

Complete the code to shadow y with a string value.

Rust
let y = 10;
let y = [1];
println!("{}", y);
Drag options to blanks, or click blank then click option'
A20
B"ten"
Cy + 5
Dtrue
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Trying to assign a number instead of a string.
Not using quotes for the string.
3fill in blank
hard

Fix the error by shadowing z with a new value inside the block.

Rust
let z = 3;
{
    let [1] = z + 2;
    println!("{}", z);
}
println!("{}", z);
Drag options to blanks, or click blank then click option'
Az_new
Bx
Cy
Dz
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a different variable name instead of shadowing.
Trying to modify z without shadowing.
4fill in blank
hard

Fill both blanks to shadow a with a new value and then shadow it again with a string.

Rust
let a = 1;
let a = [1];
let a = [2];
println!("{}", a);
Drag options to blanks, or click blank then click option'
Aa + 1
B"hello"
C"world"
Da * 2
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Trying to shadow with a number then a number again.
Not using quotes for the string shadow.
5fill in blank
hard

Fill all three blanks to shadow val by doubling, then converting to string, then appending an exclamation mark.

Rust
let val = 4;
let val = val [1] 2;
let val = val.to_string();
let val = val [2] [3];
println!("{}", val);
Drag options to blanks, or click blank then click option'
A*
B+
C"!"
D-
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using - instead of + for string concatenation.
Trying to multiply strings.