Depending on a usecase you might find that many of your “entrypoint functions” might all share the same error type. For example, if you’re making an API backend it’s likely gonna be something a status code and a message.
In that case a simple way to avoid having to pattern match every single error is to implement From<DownstreamErrorType> forMyErrorEnum, and just use the ? operator. Example:
fndo_something_with_database() ->Result<Whatever, DatabaseError> { /* ... */ }
implFrom<DatabaseError> forMyErrorType { /* ... */ }
// this would be your functionfnapi_get_something() ->Result<SomeOtherType, MyErrorType> {
letdata: Whatever = do_something_with_database()?;
// ...Ok(/* ... */)
}
Depending on a usecase you might find that many of your “entrypoint functions” might all share the same error type. For example, if you’re making an API backend it’s likely gonna be something a status code and a message.
In that case a simple way to avoid having to pattern match every single error is to implement
From<DownstreamErrorType> for MyErrorEnum, and just use the?operator. Example:fn do_something_with_database() -> Result<Whatever, DatabaseError> { /* ... */ } impl From<DatabaseError> for MyErrorType { /* ... */ } // this would be your function fn api_get_something() -> Result<SomeOtherType, MyErrorType> { let data: Whatever = do_something_with_database()?; // ... Ok(/* ... */) }EDIT: this is mentioned in the article already