# Syntax for returning early with an error

**URL:** <https://internals.rust-lang.org/t/syntax-for-returning-early-with-an-error/14181>\
**Category:** language design\
**Created:** [March 5, 2021, 3:32pm UTC](https://internals.rust-lang.org/t/syntax-for-returning-early-with-an-error/14181 "2021-03-05T15:32:01Z")\
**Posts on this page:** 3\
**Page:** 4

<div class="post-metadata">

**Author:** ![bill\_myers](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bill_myers/32/4085_2.png) [@bill\_myers](https://internals.rust-lang.org/u/bill_myers)\
**Post date:** [April 22, 2021, 10:08pm UTC](https://internals.rust-lang.org/t/syntax-for-returning-early-with-an-error/14181/62 "2021-04-22T22:08:42Z")

</div>

Actually if one were to make enum variants first class types, then this special casing could be done cleanly, and even `let x = Err(1); x?` would be a terminator, as `x` would be inferred of type Result::Err instead of Result, and then Result::Err could implement a new "TryResidual" trait that would trigger the behavior.

I think this requires a new edition though as I think it's not backward compatible to make this inference change in general, but it might actually be a viable solution, perhaps (for older editions, macros or functions could be provided that produces the enum variant explicitly).

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [April 22, 2021, 11:05pm UTC](https://internals.rust-lang.org/t/syntax-for-returning-early-with-an-error/14181/63 "2021-04-22T23:05:27Z")

</div>

> [@bill\_myers](#):
>
> what comes to my mind is to add an ad-hoc unsafe attribute on enum variants that causes `Variant(x)?` to be a terminator

I think that with never\_type we already got all we need. No need for anything fancy/new/involving-editions/etc…

```rust
#![feature(never_type)]

fn yeet<E>(e: E) -> Result<!, E> {
    Err(e)
}

fn foo() -> Result<i32, bool> {
    yeet(false)?;
    // unreachable
}

```

([playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=d0b1fcd34b0ca2224cc12a1c6ba635db))

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [July 21, 2021, 11:05pm UTC](https://internals.rust-lang.org/t/syntax-for-returning-early-with-an-error/14181/64 "2021-07-21T23:05:40Z")

</div>

Closed on July 21, 2021, 11:05pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

[Previous page](https://internals.rust-lang.org/t/syntax-for-returning-early-with-an-error/14181.md?page=3)
