0
0
Rustprogramming~20 mins

Why variables are needed in Rust - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Variable Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this Rust code?

Look at this Rust program. What will it print?

Rust
fn main() {
    let x = 5;
    let y = 10;
    let sum = x + y;
    println!("Sum is: {}", sum);
}
ASum is: 510
BSum is: 15
CSum is: 5 + 10
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint

Variables store values so you can use them later in calculations.

๐Ÿง  Conceptual
intermediate
1:30remaining
Why do we use variables in programming?

Choose the best reason why variables are important in programming.

ATo make the program run faster by skipping calculations
BTo prevent the program from using memory
CTo store and name data so it can be used and changed later
DTo make the program look colorful on the screen
Attempts:
2 left
๐Ÿ’ก Hint

Think about how you keep track of things in real life by giving them names.

โ“ Predict Output
advanced
2:00remaining
What happens if you try to use a variable before declaring it in Rust?

Consider this Rust code snippet. What will happen when you try to compile it?

Rust
fn main() {
    println!("Value is: {}", value);
    let value = 10;
}
ACompilation error: cannot find value `value` in this scope
BPrints: Value is: 10
CRuntime error: variable not found
DPrints: Value is: 0
Attempts:
2 left
๐Ÿ’ก Hint

In Rust, variables must be declared before use.

๐Ÿ”ง Debug
advanced
2:30remaining
Find the error related to variables in this Rust code

What is wrong with this Rust code snippet?

Rust
fn main() {
    let mut count = 5;
    count = "ten";
    println!("Count is {}", count);
}
ANo error, prints: Count is ten
BSyntax error: missing semicolon after assignment
CRuntime error: invalid type conversion
DType mismatch error: cannot assign a string to an integer variable
Attempts:
2 left
๐Ÿ’ก Hint

Rust variables have fixed types unless explicitly changed.

๐Ÿš€ Application
expert
2:00remaining
How many variables are created and used in this Rust program?

Count the number of variables declared and used in this Rust code.

Rust
fn main() {
    let a = 3;
    let b = 4;
    let c = a * b;
    let d = c + a;
    println!("Result: {}", d);
}
A4 variables
B3 variables
C5 variables
D2 variables
Attempts:
2 left
๐Ÿ’ก Hint

Count each let statement that creates a variable.