3 ms·
This advice mostly makes sense to me now, but coming in as a newbie, I didn't know what either `Box` or `dyn` meant, and I didn't want to just cargo-cult copy-p
by rectang 6y ago
This advice mostly makes sense to me now, but coming in as a newbie, I didn't know what either `Box` or `dyn` meant, and I didn't want to just cargo-cult copy-paste. A newcomer to the language has to deal with error handling right away, long before you need to know about either `Box` or `dyn`.
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.
- nonbirithm 6y agoYou might like 'anyhow': https://github.com/dtolnay/anyhow https://github.com/dtolnay/anyhow
- bccdee 6y agoYeah `anyhow` is basically `Result<T, Box<dyn Error>>` but ergonomic.
- lr1970 6y ago> I sort of wish there was some official `Outcome` type defined as an alias of `Result<T, Box<dyn Error>>` Would it be as easy as type OutCome<T> = Result<T, Box<dyn Error>>;
- rectang 6y agoYes! But... 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.