The purpose of our example is to read a file, age.txt, and return the number written in it, assuming that it represents some kind of age. We can encounter three errors during this process:
- Failure to read the file (maybe it doesn't exist)
- Failure to read its content as a number (it could contain text as well)
- The number could be negative
These possible error states are the possible variants of our Error enum: AgeReaderError[7]. It is usual to name the variants after the sub-errors they represent. Because a failure to read the file raises an io::Error, we name our corresponding variant AgeReaderError::Io[8]. A failure to parse a &str as an i32 raises a num::ParseIntError, so we name our encompassing variant AgeReaderError::Parse[9].
These two std errors show the naming convention of errors neatly. If you have many different errors that can be returned by a module, export them via their full name, such as num::ParseIntError. If your module only...