0
0
Rustprogramming~10 mins

If–else expression 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 assign 10 to x if condition is true, otherwise 5.

Rust
let x = if condition [1] 10 else 5;
Drag options to blanks, or click blank then click option'
A==
B{
C;
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a semicolon instead of braces after if condition.
Trying to use == instead of braces for the if block.
2fill in blank
medium

Complete the code to assign the larger of a and b to max using if–else expression.

Rust
let max = if a > b [1] a else b;
Drag options to blanks, or click blank then click option'
A;
B:
Cthen
D{
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'then' keyword which Rust does not have.
Omitting braces around the values.
3fill in blank
hard

Fix the error in the if–else expression to correctly assign a value to result.

Rust
let result = if number % 2 == 0 [1] "even" else "odd";
Drag options to blanks, or click blank then click option'
A{
B;
C:
Dthen
Attempts:
3 left
💡 Hint
Common Mistakes
Using semicolon instead of braces.
Using 'then' keyword which Rust does not support.
4fill in blank
hard

Complete the code to create a conditional expression that assigns 'positive' or 'non-positive' to status.

Rust
let status = if number {BLANK_1}} 0 { "positive" else "non-positive";
Drag options to blanks, or click blank then click option'
A>
B{
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than or equal operator in the condition.
Omitting braces after the if condition.
5fill in blank
hard

Fill all three blanks to assign a grade based on score using nested if–else expressions.

Rust
let grade = if score >= 90 [1] "A" else if score >= 80 [2] "B" else [3] "C";
Drag options to blanks, or click blank then click option'
A{
D;
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting braces around nested if blocks.
Using semicolons instead of braces.