Which is more idiomatic

Given

enum FooError {
  Bar(BarError),
  Baz,
}

enum BarError {
  A,
  B,
  C(String),
}

impl From<BarError> for FooError {
  fn from(err: BarError) -> Self {
    Self::Bar(err)
  }
}

which is better

fn foo(a: bool, b: u8) -> Result<u8, FooError> {
  use FooError as E;

  // ...

  // #1
  if b < 100 {
    return Err(E::Bar(BarError::A));
  }

  // #2
  if b < 100 {
    return Err(BarError::A.into());
  }

  // #3
  if b < 100 {
    Err(BarError::A)?;
  }

  // ...

  Ok(b)
}

I have been using the 3rd option because it is the shortest and I have to do so in a lot of places but it feels wrong since it is not immediately clear that the function can return from those lines. However, at the same time it feels right in the sense that automatic Into is a feature of ?. Later in the function I also have something like the following:

let x = match x.as_str() {
  "a" => X::A,
  "b" => X::B,

  _ => Err(BarError::C(x))?,
};

which I suppose is more clear since you can not assign the result of a match using return but would become inconsistent if I did it differently elsewhere.

not immediately clear that the function can return from those lines

It's clear that the function can return from those lines (once you know the ? Rust syntax), but I think what you meant is that it's not clear without seeing both the Err and ? pieces that the line always returns there.

I also generally go with option #3, the reason being that it's also consistent with other occurrences of ? (i.e. searching for error returns is actually facilitated by using the ? syntax). But then sometimes I also use the return syntax if the Err is simple to create. I think it's pretty much a wash. Clippy also doesn't have anything to say about it.

(BTW, sadly, rust-analyzer (at least the version that I'm using) does not show up the ? locations when highlighting return points via clicking the fn or -> keywords. Whereas the inverse works, clicking a ? does highlight the function and returns.)

I would usually write option 2 - it's clear what the error is, and that this is an unconditional return. Option 1 is more explicit, but I'd consider the extra changes mostly noise; I would also accept option 3 in code review (but wouldn't write it myself).

There's a Clippy lint, clippy::try_err in the "restriction" group related to this that would ban option 3 - this implies that option 3 is perfectly idiomatic, since if it wasn't, that lint would be moved to the "style" group, instead of staying in "restriction"[1].


  1. The restriction group is for lints that prevent the use of perfectly good library and language features, and is a mix of things that might be useful in special cases, like clippy::todo and rules that some Rust programmers like, but others would hate to be bound by. ↩︎

I'd criticize all of those in a code review due to the aliasing within the function.
This means, that the error type has a different name within the function body than in its signature, which impedes readability and doesn't help with reasoning about the code.

I also wouldn't accept #3 since the early return is not explicit enough for my taste. The ? operator has its use cases, but if the early return pattern is used, I expect the return keyword.

Since 1.98.0 I actually find this easiest to read where error-interconversion is needed:

(b < 100).ok_or(BarError::A)?;

it's certainly not "idiomatic" (yet?) due to the newness on stable but it has a very "not go" feel to it that makes me think "rust"

What I do is option 4:

  // #4
  if b < 100 {
    return Err(BarError::A)?;
  }

The return keyword has no semantic valence here (i.e. it functions precisely like option 3); it is purely for human consumption. This makes it immediately obvious there is an early return site, which I agree is useful.

If the type in the error expr doesn't match the stated return type, the default assumption is that there is a From impl between stated error types. In my code bases that's pretty much always true, so might as well leverage it to avoid some boilerplate.

At first I was going to answer #2 but I think in practice I tend to use your #4, yeah. It makes me feel a little weird to have an unconditional early return that doesn't, er, return.

That said I also lean towards "only one exit" patterns... that is, I always prefer a full if-else ladder over an "early" return, as often the "early" is only "before the next if that also returns, and the next, and the next, and then it's over", so I just write

if test(blah) {
    Err(find_reason_to_reject(blah))
} else {
    Ok(blah)
}

Even if Ok(blah) is more than a few lines of code, it has to be a Lot before just one deindent feels worth it to me. It's often not long before one has to write another else if and drag part from the else up anyways. Whitespace diffs only distract.

This is also partly informed by how often functions terminate in match, and often that is the sole expression. Restructuring code around an enum instead of an if-else ladder of, say, string comparisons, is also common.