4 ms·
> Isn't it too much clutter? I have never seen, other than like, methods on Result itself, Rust code that accepts a Result type as an argument. You are absolut
by steveklabnik 2y ago
> Isn't it too much clutter?
I have never seen, other than like, methods on Result itself, Rust code that accepts a Result type as an argument. You are absolutely right that would lead to a crazy amount of clutter.
> Imagine you need to call f(h1()) and f(h2()) where h1() and h2() return the same data type for valid data but different types of errors. What do you do?
It sort of depends on the specific different error types. Most of the code I've been writing is application level code, so I'd just be returning the errors upstream. The anyhow library makes it easy to return a "trait object" which is sort of like a virtual base class in C++, at least for these purposes. With that I could just do
f(h1()?);
f(h2()?);
The ? operator lets you return the error half of the Result to the parent. If I had to do the conversions myself, if I implemented some traits, I could do this too. That said, I would probably use a temporary for clarify:
let x = h1()?;
f(x);
let x = h2()?;
f(x);
For some cases, I may not want to implement the traits, but maybe there's some sort of common error that I'd want to return to the caller. In that case, I may
let x = h1().map_err(|e| /* do whatever conversion here */ )?;
f(x);
let x = h2().map_err(|e| /* do whatever conversion here */ )?;
f(x);
These three represent the vast majority of this kind of code I write and see in the wild.
- protomolecule 2y agoThanks for chiming in. When I looked at anyhow, I remember thinking that it's just a way to emulate in Rust the kind of error propagation that C++ has with exceptions, but manually with ?. What's your take on this?
- steveklabnik 2y agoI think that it's reasonable to look at two features designed to solve similar problems, and think they look similar. And yeah, anyhow feels closer to an exception. There's some differences though; you don't have to use anyhow, so in some senses, Rust's system here has more flexibility overall. Another is that anyhow gives you a more "semantic" backtrace by default. Here's what I mean: Consider this program: use anyhow::Result; use anyhow::bail; fn main() -> Result<()> { foo()?; Ok(()) } fn foo() -> Result<()> { bar()?; Ok(()) } fn bar() -> Result<()> { bail!("oh no"); } When you run this, you'll see Error: oh no We don't get a stacktrace, we get our error. Whenever you want to insert something into this backtrace, you can: use anyhow::bail; use anyhow::Context; use anyhow::Result; fn main() -> Result<()> { foo()?; Ok(()) } fn foo() -> Result<()> { bar().context("couldn't bar")?; Ok(()) } fn bar() -> Result<()> { bail!("oh no"); } Running this, you instead get Error: couldn't bar Caused by: oh no And if we add some context to foo: Error: was trying to foo Caused by: 0: couldn't bar 1: oh no I find this to be a lot nicer than a stacktrace. And if you want to get a stacktrace, you can do so by setting the standard environment variable.
- protomolecule 2y agoYep, semantic backtrace is very handy when you need the user to understand the error and fix input data.