Curriculum Errors, crates & tests Custom error enums
Custom error enums
Small Rust programs often use an enum for domain errors. This theme teaches error variants, carrying context, Display, and using From so ? can convert lower-level errors. Planned exercises: 1. choose an error enum. 2. recognise context-carrying variants. 3. choose From for conversion. 4. fill an Err(MyError::...). 5. fill a Display arm. 6. write a domain error enum. 7. write a conversion with From. 8. translate TS custom error classes. 9. fix an error propagation type mismatch.
Exercises
9 ready
01
pick one
A small CLI parses numbers and divides them. Pick the idiomatic
02
pick one
A parse failure should keep the offending input so the error
03
pick one
Inside a function returning Result<i32, AppError>, calling
04
fill blanks
Fill the variant name. Returns an Err carrying the
05
type one line
Fill the Display match arm for the DivideByZero variant.
06
write a program
Define enum AppError { ParseFailed(String), DivideByZero }
07
write a program
Reuse the AppError enum from slot 06. Add
08
write a program
Translate the TS custom-error class to Rust. check(x)
09
write a program
This program fails to compile because ? cannot convert