0
0
Rustprogramming~10 mins

Immutable 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 an immutable variable named x with value 5.

Rust
let [1] = 5;
Drag options to blanks, or click blank then click option'
Amut x
Blet mut x
Cvar x
Dx
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Adding mut makes the variable mutable, which is not what we want.
Using var is not valid syntax in Rust.
2fill in blank
medium

Complete the code to print the value of the immutable variable y.

Rust
let y = 10;
println!("[1]", y);
Drag options to blanks, or click blank then click option'
A"{}"
B"value"
C"y"
D"%d"
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using quotes around the variable name inside the string prints the name, not the value.
Using %d is not valid in Rust's println! macro.
3fill in blank
hard

Fix the error in the code by completing the blank to make the variable z immutable.

Rust
let [1] = 20;
z = 30; // This line causes an error
Drag options to blanks, or click blank then click option'
Amut z
Bz
Clet z
Dvar z
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Adding mut to fix the error changes the variable to mutable, which is not the goal.
Using var is invalid syntax in Rust.
4fill in blank
hard

Fill both blanks to declare an immutable variable a with value 15 and print it.

Rust
let [1] = 15;
println!("[2]", a);
Drag options to blanks, or click blank then click option'
Aa
B"{}"
C"a"
Dmut a
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using mut a makes the variable mutable, which is not needed.
Printing "a" prints the letter 'a', not the variable's value.
5fill in blank
hard

Fill all three blanks to declare an immutable variable name with value "Rust", then print it with a message.

Rust
let [1] = [2];
println!("Hello, [3]!");
Drag options to blanks, or click blank then click option'
Aname
B"Rust"
C{}
Dmut name
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using mut name makes the variable mutable.
Forgetting to add the variable as an argument to println! causes errors.