0
0
Rustprogramming~10 mins

Scope of variables 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 declare a variable inside the main function.

Rust
fn main() {
    let [1] = 5;
    println!("Value: {}", x);
}
Drag options to blanks, or click blank then click option'
A5
Bx
Clet
Dmain
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a number instead of a variable name.
Trying to use a keyword like 'let' as the variable name.
2fill in blank
medium

Complete the code to print the variable declared inside the block.

Rust
fn main() {
    {
        let y = 10;
        println!("[1]: {}", y);
    }
}
Drag options to blanks, or click blank then click option'
Ay
Bz
Cx
Dvalue
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a variable name not declared in the block.
Trying to print a variable declared outside the block.
3fill in blank
hard

Complete the code to attempt accessing the variable outside its scope and observe the error.

Rust
fn main() {
    {
        let z = 15;
    }
    println!("Value: {}", [1]);
}
Drag options to blanks, or click blank then click option'
Az
B15
Cvalue
Dlet
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Trying to use z outside its block (this is expected to error).
Using a literal number where a variable is expected.
4fill in blank
hard

Fill both blanks to declare and use a variable inside a nested block.

Rust
fn main() {
    let [1] = 20;
    {
        println!("[2] inside block: {}", [1]);
    }
}
Drag options to blanks, or click blank then click option'
Anum
Bvalue
Dnumber
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using different names for declaration and print label.
Using a variable name not declared.
5fill in blank
hard

Fill all three blanks to declare a variable, declare another based on it, and print both values.

Rust
fn main() {
    let [1] = 5;
    let [2] = [1] + 10;
    println!("Original: {}, Second: {}", [1], [2]);
}
Drag options to blanks, or click blank then click option'
Ax
By
Dz
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the same variable name for both declarations.
Mixing up variable names in the print statement.