Bird
Raised Fist0
Rustprogramming~10 mins

If expression in Rust - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print "Positive" if the number is greater than zero.

Rust
let number = 5;
if number [1] 0 {
    println!("Positive");
}
Drag options to blanks, or click blank then click option'
A<
B!=
C==
D>
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<' instead of '>' will check for negative numbers.
Using '==' checks for equality, not greater than.
2fill in blank
medium

Complete the code to assign the string "even" or "odd" based on the number.

Rust
let number = 4;
let result = if number % 2 [1] 0 { "even" } else { "odd" };
Drag options to blanks, or click blank then click option'
A==
B!=
C>
D<
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '!=' will assign 'even' to odd numbers.
Using '>' or '<' does not correctly check for evenness.
3fill in blank
hard

Fix the error in the if expression to correctly assign the maximum of two numbers.

Rust
let a = 10;
let b = 20;
let max = if a [1] b { b } else { a };
Drag options to blanks, or click blank then click option'
A<
B>
C==
D!=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '>' will assign a as max incorrectly.
Using '==' or '!=' does not find the maximum.
4fill in blank
hard

Fill in the blank to create a dictionary with word lengths for words longer than 3 letters.

Rust
let words = ["cat", "house", "dog", "elephant"];
let lengths = words.iter()
    .filter(|&word| word.len() [1] 3)
    .map(|word| (word, word.len()))
    .collect::<std::collections::HashMap<_, _>>();
Drag options to blanks, or click blank then click option'
A!=
B>
C==
D<
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<' will select shorter words.
Using '==' will select only words of length 3.
5fill in blank
hard

Fill the blanks to create a HashMap of uppercase words and their lengths for words longer than 4 letters.

Rust
let words = ["apple", "bat", "carrot", "dog"];
let result = words.iter()
    .filter(|&word| word.len() [1] 4)
    .map(|word| (word.[2](), word.len()))
    .collect::<std::collections::HashMap<_, _>>();
Drag options to blanks, or click blank then click option'
A<
B>
Cto_uppercase
Dto_lowercase
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<' will select shorter words.
Using 'to_lowercase' will convert words to lowercase instead of uppercase.

Practice

(1/5)
1. What does an if expression do in Rust?
easy
A. It chooses between code blocks based on a condition.
B. It repeats code multiple times.
C. It defines a new function.
D. It declares a variable.

Solution

  1. Step 1: Understand the purpose of if

    The if expression checks a condition to decide which code block to run.
  2. Step 2: Compare with other options

    Repeating code is done by loops, functions define behavior, and variables store data, so these are incorrect.
  3. Final Answer:

    It chooses between code blocks based on a condition. -> Option A
  4. Quick Check:

    If expression = choose code [OK]
Hint: If checks true/false to pick code [OK]
Common Mistakes:
  • Confusing if with loops
  • Thinking if declares variables
  • Mixing if with function definitions
2. Which of the following is the correct syntax for an if expression in Rust?
easy
A. if condition: { /* code */ } else: { /* code */ }
B. if condition { /* code */ } else { /* code */ }
C. if (condition) then { /* code */ } else { /* code */ }
D. if condition -> { /* code */ } else -> { /* code */ }

Solution

  1. Step 1: Recall Rust if syntax

    Rust uses if condition { ... } else { ... } without parentheses or arrows.
  2. Step 2: Check each option

    if condition { /* code */ } else { /* code */ } matches Rust syntax; others use invalid keywords or symbols.
  3. Final Answer:

    if condition { /* code */ } else { /* code */ } -> Option B
  4. Quick Check:

    Rust if syntax = braces, no parentheses [OK]
Hint: Rust if uses braces, no parentheses [OK]
Common Mistakes:
  • Adding parentheses like other languages
  • Using 'then' keyword
  • Using colons or arrows incorrectly
3. What is the output of this Rust code?
let x = 10;
let y = if x > 5 { "big" } else { "small" };
println!("{}", y);
medium
A. Compilation error
B. small
C. big
D. 10

Solution

  1. Step 1: Evaluate the condition x > 5

    Since x is 10, which is greater than 5, the condition is true.
  2. Step 2: Determine which branch runs

    The if expression returns "big" because the condition is true, so y is assigned "big".
  3. Final Answer:

    big -> Option C
  4. Quick Check:

    10 > 5 is true, so output = big [OK]
Hint: Check condition true or false to pick output [OK]
Common Mistakes:
  • Confusing output with variable x
  • Thinking else branch runs
  • Expecting numeric output
4. Identify the error in this Rust code snippet:
let number = 7;
let result = if number % 2 == 0 {
    "even"
} else {
    "odd"
};
println!("{}", result);
medium
A. No error, code runs correctly
B. Mismatched types in if and else branches
C. Missing semicolon after if expression
D. Using % operator incorrectly

Solution

  1. Step 1: Check syntax and types

    The if expression returns string slices "even" or "odd", both same type, so no type mismatch.
  2. Step 2: Verify semicolons and operators

    The semicolon after the if expression is present, and the modulo operator is used correctly.
  3. Final Answer:

    No error, code runs correctly -> Option A
  4. Quick Check:

    Correct syntax and types = no error [OK]
Hint: Check types and semicolons carefully [OK]
Common Mistakes:
  • Thinking semicolon is missing
  • Confusing types in branches
  • Misusing modulo operator
5. How can you use an if expression to assign a value to a variable based on multiple conditions in Rust? Choose the correct code snippet.
hard
A. let grade = if score >= 90 -> 'A' else if score >= 80 -> 'B' else -> 'C';
B. let grade = if (score >= 90) then 'A' else if (score >= 80) then 'B' else 'C';
C. let grade = if score >= 90: 'A' elif score >= 80: 'B' else: 'C';
D. let grade = if score >= 90 { 'A' } else if score >= 80 { 'B' } else { 'C' };

Solution

  1. Step 1: Understand Rust's if-else if syntax

    Rust uses if condition { value } and else if condition { value } blocks with braces, no keywords like then or arrows.
  2. Step 2: Check each option for syntax correctness

    let grade = if score >= 90 { 'A' } else if score >= 80 { 'B' } else { 'C' }; uses correct Rust syntax; others use invalid keywords or punctuation.
  3. Final Answer:

    let grade = if score >= 90 { 'A' } else if score >= 80 { 'B' } else { 'C' }; -> Option D
  4. Quick Check:

    Rust if-else if uses braces, no then [OK]
Hint: Use braces and else if without extra keywords [OK]
Common Mistakes:
  • Adding 'then' keyword
  • Using colons or arrows
  • Incorrect else if syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard