100-exercises-to-learn-rust icon indicating copy to clipboard operation
100-exercises-to-learn-rust copied to clipboard

Discrepancy between example in 5.14 and solution

Open ericzundel opened this issue 10 months ago • 0 comments

This isn't really a bug, but both my partner and I learning Rust felt that 5.14 was a beast! I had to cheat and look at the solution. My partner used an LLM and got an answer very much unlike what we had learned to date.

I was confused by something I saw in the solution brand. The example for how to use #from in 5.14 reads as follows:

#[derive(Error, Debug)]
pub enum MyError {
    #[error("Failed to connect to the database")]
    DatabaseError {
        #[from]
        inner: std::io::Error
    }
}

Note that the syntax in the example uses the structure form of associating data with the enum But in the solution, Database Error uses parens

[derive(Debug, thiserror::Error)]
pub enum TicketNewError {
    ...
    #[error("{0}")]
    InvalidStatus(#[from] ParseStatusError),
}

Tuples or tuple like syntax is covered in the Nullability lesson 5.5 , but never used in an exercise before this one. Tricky!

ericzundel avatar Feb 01 '25 01:02 ericzundel