Curriculum Errors, crates & tests Result and the question mark operator
Result and the question mark operator
Recoverable errors are values: Result<T, E>. The ? operator either unwraps Ok(T) or returns the Err(E) early from the current function. Planned exercises: 1. choose Result<T, E>. 2. recognise Ok and Err. 3. choose ? over nested matches for propagation. 4. fill an Ok(value). 5. fill a function return type. 6. write a parser helper. 7. write a function with two ? calls. 8. translate TS throw/catch code. 9. fix a function whose return type cannot use ? yet.
Exercises
9 ready
01
pick one
A function MIGHT succeed with an i32 or fail with a String
02
pick one
Pick the pair of variant names that ship with Result<T, E>.
03
pick one
Pick the strongest reason to use the ? operator instead of
04
fill blanks
Fill the variant name that constructs a successful Result
05
fill blanks
Fill the return type. The function might succeed with i32 or
06
write a program
Write parse_age(s: &str) -> Result<i32, String> that parses
07
write a program
Write combine(a: &str, b: &str) -> Result<i32, String> that
08
write a program
Translate the TS throw/catch pattern. parse_age(s) returns
09
write a program
This program fails to compile with error[E0277]: the ?`