Error Handling in Rust: Result, Option, and Panic Quiz Quiz

Explore essential Rust error handling concepts with this quiz, focusing on the Result and Option types, as well as panic scenarios. Sharpen your understanding of robust, safe Rust code practices using common error handling patterns.

  1. Identifying Result Usage

    Which Rust type is typically used to return either a value on success or an error on failure, such as in a function that opens a file?

    1. Option
    2. Result
    3. ErrRes
    4. PanicResult

    Explanation: The Result type is designed for functions that can return either a value (Ok) or an error (Err). Option is used for values that may be absent but not due to errors, ErrRes and PanicResult are not standard Rust types. Therefore, Result is the correct choice.

  2. Using Option Correctly

    If a Rust function might return either an integer or nothing, such as searching for an item in a list, which type should it return?

    1. Maybe
    2. OptionResult
    3. Result
    4. Option

    Explanation: Option is used when a value might be present or absent, like when searching for something that might not be found. Result is for error handling, Maybe and OptionResult are not built-in Rust types. Thus, Option fits this scenario.

  3. Result Type Variants

    What are the two possible variants of the standard Result type in Rust?

    1. Some and None
    2. True and False
    3. Ok and Err
    4. Valid and Invalid

    Explanation: Ok and Err are the two variants of the Result type, signaling success and error respectively. Some and None are used with Option, while Valid, Invalid, True, and False are not standard Result variants. Therefore, Ok and Err are correct.

  4. Unwrapping Option

    What happens if you call unwrap() on a Rust Option value that is None?

    1. It returns an empty Option
    2. It returns 0
    3. The code silently continues
    4. The program panics

    Explanation: Calling unwrap() on a None Option causes a panic, terminating the program. It does not return 0 or an empty Option, nor does the code continue without issue. Therefore, a panic is the correct outcome.

  5. Using match for Error Handling

    In Rust, which construct is commonly used to handle different cases of Result or Option, such as handling Ok/Err or Some/None?

    1. switch
    2. select
    3. match
    4. choose

    Explanation: The match statement is used in Rust to handle different variants like Ok/Err or Some/None. Switch, choose, and select are not valid constructs in Rust for this purpose. Thus, match is the correct answer.

  6. Purpose of panic!

    What is the effect of calling the panic! macro in a Rust program?

    1. It ignores the error and runs
    2. It returns None
    3. The program exits with an error
    4. It logs a warning and continues

    Explanation: Using panic! causes the program to immediately exit and display an error. It does not simply log a warning, return None, or continue running. The panic! macro is specifically meant for unrecoverable errors.

  7. Result Error Propagation

    Which Rust operator is commonly used to return early from a function if a Result is an Err, propagating the error to the caller?

    1. $
    2. !
    3. ?
    4. ~

    Explanation: The question mark operator (? ) is used for propagating errors in functions that return Result or Option. The other symbols are not error propagation operators in Rust. Therefore, ? is the correct option.

  8. Checking for Some Variant

    Given let opt = Some(10);, which method checks if opt contains a value in Rust?

    1. has_value()
    2. check_some()
    3. is_some()
    4. is_valid()

    Explanation: The is_some() method returns true if the Option contains a value. has_value(), is_valid(), and check_some() are not standard methods on Option. So, is_some() is the accurate choice.

  9. Converting Option to Result

    Which Option method in Rust allows you to turn a Some(value) into an Ok(value) or None into an Err with a custom error?

    1. option_to_result
    2. unwrap_or
    3. ok_or
    4. as_result

    Explanation: The ok_or method converts an Option to a Result, assigning Ok for Some and Err for None. unwrap_or is used to provide a default value, while as_result and option_to_result are not actual Option methods. Thus, ok_or is correct.

  10. panic! vs. Result

    Why is it recommended to use Result over panic! for recoverable errors in Rust?

    1. panic! can be used for both recoverable and unrecoverable errors
    2. Result is only for debugging
    3. Result allows the program to handle errors without crashing
    4. panic! always performs better than Result

    Explanation: Result enables functions to signal and handle errors gracefully, allowing recovery or alternate action without terminating the program. panic! causes a crash, and is not meant for recoverable errors. Result is not just for debugging, and panic! should only be used for unrecoverable errors.