Bird
Raised Fist0
Rustprogramming~20 mins

Operator precedence in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Operator Precedence Master
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 involving mixed operators?
Consider the following Rust code snippet. What will it print when run?
Rust
fn main() {
    let result = 3 + 4 * 2;
    println!("{}", result);
}
A11
BError
C10
D14
Attempts:
2 left
๐Ÿ’ก Hint
Remember that multiplication (*) has higher precedence than addition (+).
โ“ Predict Output
intermediate
2:00remaining
What is the output of this Rust code with logical and comparison operators?
What does this Rust program print?
Rust
fn main() {
    let a = true;
    let b = false;
    let c = a || b && false;
    println!("{}", c);
}
Atrue
Bfalse
CCompilation error
Dpanic at runtime
Attempts:
2 left
๐Ÿ’ก Hint
Logical AND (&&) has higher precedence than logical OR (||).
โ“ Predict Output
advanced
2:00remaining
What is the output of this Rust code with mixed bitwise and comparison operators?
Analyze the following Rust code and select the output it produces.
Rust
fn main() {
    let x = 5;
    let y = 3;
    let z = (x & y) == 1;
    println!("{}", z);
}
Afalse
Bpanic at runtime
Ctrue
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint
Bitwise AND (&) has higher precedence than equality (==).
โ“ Predict Output
advanced
2:00remaining
What is the output of this Rust code with assignment and arithmetic operators?
What does this Rust program print?
Rust
fn main() {
    let mut a = 2;
    a += 3 * 4;
    println!("{}", a);
}
A20
B14
CError: cannot assign to immutable variable
D10
Attempts:
2 left
๐Ÿ’ก Hint
Multiplication happens before addition assignment.
โ“ Predict Output
expert
2:00remaining
What is the output of this Rust code with mixed shift and arithmetic operators?
Analyze this Rust code and select the output it produces.
Rust
fn main() {
    let a = 1 + 2 << 3;
    println!("{}", a);
}
A9
BCompilation error
C16
D24
Attempts:
2 left
๐Ÿ’ก Hint
Addition (+) has higher precedence than bitwise shift (<<).

Practice

(1/5)
1. In Rust, which operator has the highest precedence among these: +, *, -, /?
easy
A. Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-).
B. Addition (+) and subtraction (-) have higher precedence than multiplication (*) and division (/).
C. All operators have the same precedence and are evaluated left to right.
D. Subtraction (-) has the highest precedence among all.

Solution

  1. Step 1: Recall Rust operator precedence rules

    Multiplication (*) and division (/) operators have higher precedence than addition (+) and subtraction (-).
  2. Step 2: Compare given operators

    Among the given operators, * and / are evaluated before + and - in expressions.
  3. Final Answer:

    Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-). -> Option A
  4. Quick Check:

    *, / > +, - [OK]
Hint: Remember: multiply/divide before add/subtract [OK]
Common Mistakes:
  • Thinking addition has higher precedence than multiplication
  • Assuming all operators have equal precedence
  • Confusing subtraction precedence with multiplication
2. Which of the following Rust expressions is correctly written to add 2 and 3, then multiply the result by 4?
easy
A. 2 + 3 * 4
B. (2 + 3) * 4
C. 2 + (3 * 4)
D. 2 * 3 + 4

Solution

  1. Step 1: Understand operator precedence and parentheses

    Multiplication (*) has higher precedence than addition (+), so without parentheses, multiplication happens first.
  2. Step 2: Check each expression

    (2 + 3) * 4 uses parentheses to add 2 and 3 first, then multiplies by 4, which matches the requirement.
  3. Final Answer:

    (2 + 3) * 4 -> Option B
  4. Quick Check:

    Parentheses force addition first [OK]
Hint: Use parentheses to change default order [OK]
Common Mistakes:
  • Ignoring parentheses and relying on default precedence
  • Misplacing parentheses causing wrong order
  • Assuming left-to-right always applies
3. What is the output of this Rust code?
let result = 10 - 4 / 2 + 3 * 2;
println!("{}", result);
medium
A. 13
B. 10
C. 11
D. 14

Solution

  1. Step 1: Apply operator precedence to expression

    Division and multiplication happen before addition and subtraction:
    4 / 2 = 2
    3 * 2 = 6
  2. Step 2: Calculate left to right with results

    Expression becomes: 10 - 2 + 6
    10 - 2 = 8
    8 + 6 = 14
  3. Final Answer:

    14 -> Option D
  4. Quick Check:

    */ before +- gives 14 [OK]
Hint: Calculate * and / first, then + and - left to right [OK]
Common Mistakes:
  • Doing operations strictly left to right ignoring precedence
  • Adding before multiplying
  • Miscomputing division or multiplication
4. Identify the error in this Rust expression:
let value = 5 + 3 * / 2;
medium
A. No error, expression is valid
B. Logical error: multiplication should be addition
C. Syntax error due to misplaced operator '/'
D. Runtime error due to division by zero

Solution

  1. Step 1: Analyze the expression syntax

    The expression has * / operators together without a number between them, which is invalid syntax in Rust.
  2. Step 2: Confirm error type

    This causes a syntax error because Rust expects an operand between operators.
  3. Final Answer:

    Syntax error due to misplaced operator '/' -> Option C
  4. Quick Check:

    Operators must have operands between them [OK]
Hint: Check for missing operands between operators [OK]
Common Mistakes:
  • Assuming expression is valid without checking syntax
  • Confusing syntax error with runtime error
  • Ignoring operator placement rules
5. Given the Rust expression:
let x = 2 + 3 * 4 - 6 / 3 + (8 - 5) * 2;

What is the correct value of x after evaluation?
hard
A. 18
B. 20
C. 19
D. 21

Solution

  1. Step 1: Evaluate parentheses and high precedence operators

    Calculate inside parentheses:
    (8 - 5) = 3
    Then multiplication and division:
    3 * 4 = 12
    6 / 3 = 2
    3 * 2 = 6 (from parentheses result * 2)
  2. Step 2: Substitute and calculate addition and subtraction left to right

    Expression becomes: 2 + 12 - 2 + 6
    2 + 12 = 14
    14 - 2 = 12
    12 + 6 = 18
  3. Final Answer:

    18 -> Option A
  4. Quick Check:

    Parentheses and */ first, then +- left to right [OK]
Hint: Calculate parentheses first, then * and /, then + and - left to right [OK]
Common Mistakes:
  • Ignoring parentheses and calculating in wrong order
  • Doing addition before multiplication
  • Mixing up left-to-right evaluation for + and -
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