0
0
Rustprogramming~5 mins

Immutable variables in Rust - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean when a variable is immutable in Rust?
An immutable variable is one whose value cannot be changed after it is set. Once assigned, you cannot modify it.
Click to reveal answer
beginner
How do you declare an immutable variable in Rust?
By default, variables declared with <code>let</code> are immutable. For example: <code>let x = 5;</code> creates an immutable variable <code>x</code>.
Click to reveal answer
beginner
What happens if you try to change the value of an immutable variable in Rust?
The Rust compiler will give an error and prevent the program from compiling because immutable variables cannot be changed.
Click to reveal answer
beginner
How can you make a variable mutable in Rust?
You add the keyword <code>mut</code> when declaring the variable, like <code>let mut x = 5;</code>. This allows you to change <code>x</code> later.
Click to reveal answer
intermediate
Why does Rust use immutable variables by default?
Immutable variables help prevent bugs by avoiding accidental changes. They make code safer and easier to understand.
Click to reveal answer
In Rust, what keyword do you use to declare a variable that can be changed?
Avar
Blet
Cmut
Dchange
What will happen if you try to assign a new value to an immutable variable in Rust?
AThe compiler will give an error.
BThe old value will be kept and the new value ignored.
CThe variable will automatically become mutable.
DThe program will compile and run normally.
Which of these is the correct way to declare an immutable variable with value 10 in Rust?
Alet mut x = 10;
Bconst x = 10;
Cvar x = 10;
Dlet x = 10;
Why does Rust prefer immutable variables by default?
ATo make code run faster.
BTo prevent accidental changes and bugs.
CBecause mutable variables are not allowed.
DTo save memory space.
What keyword is NOT used to declare variables in Rust?
Avar
Bmut
Clet
Dconst
Explain what immutable variables are in Rust and how they affect your code.
Think about why Rust wants you to avoid changing variables unless you say so.
You got /4 concepts.
    Describe how to declare a mutable variable in Rust and why you might want to do that.
    Remember, mut means you can change the variable later.
    You got /4 concepts.