Rust: Structuring and handling errors in 2020 (2020)
nick.groenen.me
nick.groenen.me
https://blog.rust-lang.org/inside-rust/2020/11/23/What-the-e...
(Disclaimer: I'm not involved with this group in any way.)
I sort of wish there was some official `Outcome` type defined as an alias of `Result<T, Box<dyn Error>>`:
fn main() -> OutCome<()> {
foo()?;
Ok(())
}
And then all the crates that offer their own `Result` types would have offered an `Outcome` insteaad: `io::Outcome`, `fmt::Outcome`, etc.It's not that big a deal, eventually we'll all struggle through — but `Box<dyn Error>` is a bit confusing at first.
Would it be as easy as
type OutCome<T> = Result<T, Box<dyn Error>>;
1) If I type that line myself, I want to know what `Box<dyn Error>` means.
2) For an audience of mixed levels of Rust expertise, the `Box<dyn Error>` idiom is on average clearer, because the more experienced Rustaceans will have seen it before while `Outcome` will be unfamiliar and in need of explanation.
I thought about publishing a crate with just that one line, but there's already a slew of other similar solutions out there (anyhow, any-error, etc...). There's not that much to gain.
I just think it's worth pointing out that it's weird to require newbies to understand `Box` and `dyn` in order to handle errors.
[error-chain]: https://crates.io/crates/error-chain