Bird
Raised Fist0
Rustprogramming~10 mins

Expression evaluation 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 evaluate the sum of two numbers and print the result.

Rust
fn main() {
    let a = 5;
    let b = 3;
    let result = a [1] b;
    println!("Result: {}", result);
}
Drag options to blanks, or click blank then click option'
A*
B-
C+
D/
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using subtraction - instead of addition.
Using multiplication * or division / by mistake.
2fill in blank
medium

Complete the code to calculate the remainder when 10 is divided by 3.

Rust
fn main() {
    let x = 10;
    let y = 3;
    let remainder = x [1] y;
    println!("Remainder: {}", remainder);
}
Drag options to blanks, or click blank then click option'
A%
B/
C+
D*
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using division / instead of remainder %.
Using multiplication or addition by mistake.
3fill in blank
hard

Fix the error in the expression to correctly calculate the power of 2 raised to 3.

Rust
fn main() {
    let base = 2;
    let exponent = 3;
    let power = base.[1](exponent);
    println!("Power: {}", power);
}
Drag options to blanks, or click blank then click option'
A^
B**
C.pow()
Dpow
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using ^ or ** which are not power operators in Rust.
Trying to use a function name without method call syntax.
4fill in blank
hard

Fill in the blank to create a HashMap that maps words to their lengths only if length is greater than 3.

Rust
fn main() {
    let words = vec!["apple", "cat", "banana", "dog"];
    let lengths = words.iter().filter(|&&word| word.len() [1] 3).map(|&word| (word, word.len())).collect::<std::collections::HashMap<_, _>>();
    println!("{:?}", lengths);
}
Drag options to blanks, or click blank then click option'
A!=
B>
C==
D<
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using < or == which filter wrong words.
Using != which does not check length properly.
5fill in blank
hard

Fill in the two blanks to create a HashMap from words to their uppercase forms only if the word length is less than 6.

Rust
fn main() {
    let words = vec!["apple", "cat", "banana", "dog"];
    let map = words.iter()
        .filter(|&&word| word.len() [1] 6)
        .map(|&word| (word, word.[2]()))
        .collect::<std::collections::HashMap<_, _>>();
    println!("{:?}", map);
}
Drag options to blanks, or click blank then click option'
A>
Bto_uppercase
C<
Dto_lowercase
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using > instead of < in the filter.
Using to_lowercase() instead of to_uppercase().

Practice

(1/5)
1. What does the following Rust expression do?
let result = 5 + 3 * 2;
Choose the correct description.
easy
A. Calculates 3 times 2, then adds 5
B. Multiplies 5 by 3, then adds 2
C. Calculates 5 plus 3, then multiplies by 2
D. Adds 5, 3, and 2 together

Solution

  1. Step 1: Understand operator precedence in Rust

    Multiplication (*) has higher precedence than addition (+), so 3 * 2 is calculated first.
  2. Step 2: Calculate the expression step-by-step

    3 * 2 = 6, then 5 + 6 = 11.
  3. Final Answer:

    Calculates 3 times 2, then adds 5 -> Option A
  4. Quick Check:

    5 + (3 * 2) = 11 [OK]
Hint: Remember multiplication before addition in expressions [OK]
Common Mistakes:
  • Adding numbers left to right ignoring precedence
  • Multiplying after addition
  • Confusing operator precedence
2. Which of the following is the correct way to assign the result of 4 + 7 to a variable named sum in Rust?
easy
A. let sum : 4 + 7;
B. sum = let 4 + 7;
C. let sum = 4 + 7;
D. let sum == 4 + 7;

Solution

  1. Step 1: Recall Rust variable assignment syntax

    In Rust, use let variable = expression; to assign a value.
  2. Step 2: Check each option for correct syntax

    let sum = 4 + 7; uses correct syntax: let sum = 4 + 7;. Others have syntax errors.
  3. Final Answer:

    let sum = 4 + 7; -> Option C
  4. Quick Check:

    Correct Rust assignment syntax [OK]
Hint: Use 'let var = expression;' to assign values [OK]
Common Mistakes:
  • Using '==' instead of '=' for assignment
  • Placing 'let' after variable name
  • Using ':' instead of '=' for assignment
3. What is the output of this Rust code?
fn main() {
    let x = 10;
    let y = 3;
    let result = x / y + x % y;
    println!("{}", result);
}
medium
A. 13
B. 3
C. 1
D. 4

Solution

  1. Step 1: Calculate integer division and remainder

    10 / 3 = 3 (integer division), 10 % 3 = 1 (remainder).
  2. Step 2: Add the results

    3 + 1 = 4.
  3. Final Answer:

    4 -> Option D
  4. Quick Check:

    10 / 3 + 10 % 3 = 4 [OK]
Hint: Remember integer division drops decimals, % gives remainder [OK]
Common Mistakes:
  • Treating division as float division
  • Confusing remainder with division result
  • Adding operands incorrectly
4. Identify the error in this Rust code snippet:
fn main() {
    let a = 5;
    let b = 0;
    let c = a / b;
    println!("{}", c);
}
medium
A. Division by zero causes a runtime panic
B. Variable 'c' is not declared properly
C. Missing semicolon after 'let b = 0'
D. println! macro syntax is incorrect

Solution

  1. Step 1: Analyze the division operation

    The code divides 'a' by 'b', where 'b' is zero.
  2. Step 2: Understand Rust runtime behavior

    Dividing by zero in Rust causes a runtime panic (crash), not a compile error.
  3. Final Answer:

    Division by zero causes a runtime panic -> Option A
  4. Quick Check:

    Division by zero panics in Rust [OK]
Hint: Check for zero divisor before division to avoid panic [OK]
Common Mistakes:
  • Thinking division by zero is a compile error
  • Ignoring runtime panics
  • Assuming variable declaration errors
5. Given a list of numbers, which Rust expression correctly calculates the sum of all even numbers?
let numbers = [1, 2, 3, 4, 5, 6];
let sum_even = /* fill here */;
println!("{}", sum_even);

Choose the correct expression to replace /* fill here */.
hard
A. numbers.iter().map(|x| x * 2).sum::<i32>()
B. numbers.iter().filter(|&&x| x % 2 == 0).sum::<i32>()
C. numbers.iter().filter(|&x| x % 2 != 0).sum::<i32>()
D. numbers.iter().sum::<i32>()

Solution

  1. Step 1: Understand the goal

    We want to sum only even numbers from the list.
  2. Step 2: Analyze each option

    numbers.iter().filter(|&&x| x % 2 == 0).sum::<i32>() filters even numbers (x % 2 == 0) and sums them. numbers.iter().map(|x| x * 2).sum::<i32>() doubles all numbers then sums (wrong). numbers.iter().filter(|&x| x % 2 != 0).sum::<i32>() sums odd numbers (wrong). numbers.iter().sum::<i32>() sums all numbers (wrong).
  3. Final Answer:

    numbers.iter().filter(|&&x| x % 2 == 0).sum::<i32>() -> Option B
  4. Quick Check:

    Filter evens then sum [OK]
Hint: Filter even numbers before summing with iterator methods [OK]
Common Mistakes:
  • Summing all numbers without filtering
  • Filtering odd numbers instead of even
  • Mapping values incorrectly before sum
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