A. Cannot access self.0 because enum variant is not destructured.
B. Missing implementation of Debug trait.
C. Display trait cannot be implemented for enums.
D. write! macro requires a string literal, not a variable.
Solution
Step 1: Check how self.0 is accessed
In an enum, you cannot directly access self.0 without matching or destructuring the variant.
Step 2: Understand correct pattern
You must match self to get the inner std::io::Error value before using it in write!.
Final Answer:
Cannot access self.0 because enum variant is not destructured. -> Option A
Quick Check:
Enum variant fields need matching before access [OK]
Hint: Match enum variants before accessing inner fields [OK]
Common Mistakes:
Trying to access enum fields directly without match
Thinking Display can't be implemented for enums
Assuming Debug trait is mandatory for Display
5. You want to create a custom error type that can hold either an IO error or a parsing error, and provide user-friendly messages. Which of these is the best complete approach?
hard
A. Create separate error structs for IO and parse errors without enums, and use panic! for messages.
B. Use a struct with two optional fields for IO and parse errors, and implement Display only.
C. Define an enum with variants for each error type, implement Display with match arms for messages, and implement std::error::Error.
D. Use a type alias for Result with String errors and skip custom error types.
Solution
Step 1: Choose enum for multiple error types
An enum cleanly groups different error variants like IO and parse errors.
Step 2: Implement Display and std::error::Error traits
Display provides friendly messages; implementing std::error::Error integrates with Rust error handling.
Final Answer:
Define an enum with variants for each error type, implement Display with match arms for messages, and implement std::error::Error. -> Option C