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.
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?
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.
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?
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.
What are the two possible variants of the standard Result type in Rust?
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.
What happens if you call unwrap() on a Rust Option value that is None?
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.
In Rust, which construct is commonly used to handle different cases of Result or Option, such as handling Ok/Err or Some/None?
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.
What is the effect of calling the panic! macro in a Rust program?
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.
Which Rust operator is commonly used to return early from a function if a Result is an Err, propagating the error to the caller?
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.
Given let opt = Some(10);, which method checks if opt contains a value in Rust?
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.
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?
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.
Why is it recommended to use Result over panic! for recoverable errors in Rust?
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.