Option, Result & ? Questions with Answers

19 practice questions on Option, Result & ? (Rust) with answers and step-by-step solutions. Try 10 samples below, then practise all 19 free in the AptiNest app.

Sample questions

Question 1Easy

How does .expect("msg") differ from .unwrap()?

  1. It logs the message and returns a default value instead of panicking
  2. It never panics
  3. It returns a Result
  4. It panics with the given custom message

Answer: D. It panics with the given custom message

Explanation

A descriptive message makes the panic easier to diagnose.

Step-by-step solution

  1. Expect works like unwrap but takes a message.
  2. On None or Err it panics with that message.
  3. So option D is the correct answer.

Why the other options are wrong

  • A. Expect does not log or return a default; it panics.
  • B. Expect can still panic, so it never panicking is false.
  • C. Expect returns the inner value, not a Result.

Tip: Use expect with a clear message so a panic is easy to diagnose.

Question 2Easy

What value represents 'no value' for an Option<i32>?

  1. 0
  2. null
  3. Nil
  4. None

Answer: D. None

Explanation

Option<i32> is either Some(n) or None; Rust has no null in safe code.

Step-by-step solution

  1. Option i32 is either Some holding a number or None.
  2. None is the value that stands for no value.
  3. So option D is the correct answer.

Why the other options are wrong

  • A. 0 is a real number, not the absence of a value.
  • B. Rust has no null in safe code.
  • C. Nil is not a Rust value.

Tip: Match on Some and None to handle a missing value.

Question 3Easy

What is printed?

let x: Option<i32> = None;
println!("{}", x.unwrap_or(7));
  1. 0
  2. None
  3. It panics
  4. 7

Answer: D. 7

Explanation

unwrap_or(7) returns the contained value or 7 when the option is None.

Step-by-step solution

  1. x is None.
  2. unwrap_or supplies the default.
  3. 7 is printed.

Why the other options are wrong

  • A. 0 is only for unwrap_or_default.
  • B. The value is unwrapped, so None is not printed.
  • C. unwrap_or never panics.

Question 4Easy

What type does "42".parse::<i32>() return?

  1. A plain i32 value, since parsing always succeeds
  2. Result<i32, ParseIntError>
  3. Option<i32> (no error detail kept)
  4. Result<String, i32>

Answer: B. Result<i32, ParseIntError>

Explanation

Parsing can fail, so parse returns a Result with the number on success and a ParseIntError otherwise.

Step-by-step solution

  1. Parsing may fail.
  2. Result carries the error.
  3. Ok type is i32.

Why the other options are wrong

  • A. Parsing can fail, so it is not a bare i32.
  • C. It carries an error, so Result is used, not Option.
  • D. The success type is i32, the error is ParseIntError.

Question 5Medium

Where can the? operator be used?

  1. In functions returning Result or Option (or compatible types)
  2. In any function, including those returning ()
  3. Only inside closures, never inside ordinary named functions or methods of a struct
  4. Only in main

Answer: A. In functions returning Result or Option (or compatible types)

Explanation

? needs somewhere to return the error or None to; main can return Result to use it.

Step-by-step solution

  1. The? operator returns early with the error or None from the current function.
  2. That needs a return type that can hold the error or None, such as Result or Option.
  3. So option A is the correct answer.

Why the other options are wrong

  • B. A function returning unit cannot return an error, so? cannot be used there.
  • C. ? works in ordinary named functions and methods, not only closures.
  • D. ? is not limited to main; any function returning Result or Option can use it.

Tip: Use? only in a function that returns Result or Option.

Question 6Medium

Why does this fail to compile?

fn main() {
    let n: i32 = "5".parse()?;
    println!("{}", n);
}
  1. main returns (), so ? has nowhere to return
  2. parse cannot return an i32 for any string
  3. println! cannot print an i32
  4. ? is only allowed on Option

Answer: A. main returns (), so ? has nowhere to return

Explanation

? early-returns the error, so the function's return type must be able to hold it. Declaring fn main() -> Result<(), Box<dyn std::error::Error>> fixes it.

Step-by-step solution

  1. ? returns errors upward.
  2. main returns ().
  3. Type mismatch, so compile error.

Why the other options are wrong

  • B. parse::<i32> can produce an i32.
  • C. Printing integers is fine.
  • D. ? works on Result and Option.

Question 7Medium

What does this print?

let r = Some(5).and_then(|x| if x > 3 { Some(x * 2) } else { None });
println!("{:?}", r);
  1. Some(10)
  2. The bare number 10 without Some
  3. Some(5) (the closure leaves it unchanged)
  4. None

Answer: A. Some(10)

Explanation

and_then runs the closure on the inner 5; since 5 > 3 it returns Some(10).

Step-by-step solution

  1. Option is Some(5).
  2. Closure checks 5 > 3.
  3. Returns Some(10).

Why the other options are wrong

  • B. {:?} keeps the Some wrapper.
  • C. The closure doubles the value.
  • D. 5 > 3 is true, so Some is returned.

Question 8Medium

In a function returning Option<i32>, what does ? do when applied to a None?

  1. Panics straight away with a message
  2. Replaces None with a default value of 0 and goes on
  3. Continues running with an empty placeholder value
  4. Returns None from the function immediately

Answer: D. Returns None from the function immediately

Explanation

? on None early-returns None from the enclosing function; on Some it unwraps the value.

Step-by-step solution

  1. Check the value.
  2. If None, return None.
  3. Otherwise unwrap.

Why the other options are wrong

  • A. ? never panics.
  • B. No default is inserted.
  • C. Execution does not continue.

Question 9Hard

What does if let Some(x) = opt { ... } do?

  1. Loops while opt is Some, binding each value to x in turn
  2. Assigns Some(x) to opt
  3. Runs the block only if opt is Some, binding the inner value to x
  4. Panics if opt is None

Answer: C. Runs the block only if opt is Some, binding the inner value to x

Explanation

if let is a concise match for a single pattern.

Step-by-step solution

  1. If let Some(x) = opt matches the pattern against opt.
  2. If opt is Some, the block runs with x bound to the inner value.
  3. If opt is None, the block is skipped, so option C is the correct answer.

Why the other options are wrong

  • A. If let runs the block once, not in a loop.
  • B. The statement does not assign anything to opt.
  • D. None does not cause a panic; the block is just skipped.

Tip: If let is a short match for one pattern.

Question 10Hard

How does ? convert a std::io::Error into a custom error type in a function returning Result<T, MyError>?

  1. Through an impl From<io::Error> for MyError
  2. Automatically, for any two error types
  3. It cannot; the error types must be identical or the code will not compile
  4. Via the Display trait

Answer: A. Through an impl From<io::Error> for MyError

Explanation

? calls From::from on the error, so implementing From enables seamless propagation.

Step-by-step solution

  1. ? calls From::from on the error before returning it.
  2. An impl From io::Error for MyError tells the compiler how to convert it.
  3. So option A is the correct answer.

Why the other options are wrong

  • B. Conversion is not automatic for any two types; a From impl is needed.
  • C. The types do not need to be identical when a From impl exists.
  • D. Display is for printing the error, not converting it.

Tip: Implement From for each error type you want? to convert into your error enum.

Practise all 19 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Rust topics · All topics