# The Fall-Through Proposal

**URL:** <https://internals.rust-lang.org/t/the-fall-through-proposal/13792>\
**Category:** language design\
**Created:** [January 9, 2021, 3:40pm UTC](https://internals.rust-lang.org/t/the-fall-through-proposal/13792 "2021-01-09T15:40:44Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [January 9, 2021, 3:40pm UTC](https://internals.rust-lang.org/t/the-fall-through-proposal/13792/1 "2021-01-09T15:40:45Z")

</div>

If you're trying to optimize an enum's size it can be nice to use different variants instead of booleans. But it comes with a tradeoff: code reuse.

So you have this large enum with things like `StringKey` and `NonValidatingStringKey` and you have to copy-paste the code for `StringKey` into the `NonValidatingStringKey` match case because they do slightly different things, e.g.

```rust
match foo {
  StringKey(x) => {
    //code here
    return option.map(Some).ok_or_else(|| ValidationError(etc, etc))
  },
  NonValidatingStringKey(x) => {
    //pretty much exact same code here
    return Ok(option)
  }
}

```

It'd be nice if you could do something like this:

```rust
match foo {
  StringKey(x) as let skip=false | NonValidatingStringKey(x) as let skip=true => {
    //code here
    if skip {
      return Ok(option)
    } else {
      return option.map(Some).ok_or_else(|| ValidationError(etc, etc))
    }
  }
}

```

So you get all of the benefits of fallthrough (aka code reuse) with none of the drawbacks (because it's not fallthrough).

---

<div class="post-metadata">

**Author:** ![Aloso](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aloso/32/5039_2.png) [@Aloso](https://internals.rust-lang.org/u/Aloso)\
**Post date:** [January 9, 2021, 4:06pm UTC](https://internals.rust-lang.org/t/the-fall-through-proposal/13792/2 "2021-01-09T16:06:59Z")

</div>

You can do

```rust
let (skip, x) = match foo {
    StringKey(x) => (false, x),
    NonValidatingStringKey(x) => (true, x),
}
// code here

```

---

<div class="post-metadata">

**Author:** ![Aloso](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aloso/32/5039_2.png) [@Aloso](https://internals.rust-lang.org/u/Aloso)\
**Post date:** [January 9, 2021, 4:15pm UTC](https://internals.rust-lang.org/t/the-fall-through-proposal/13792/3 "2021-01-09T16:15:07Z")

</div>

I believe that `if let` guards are planned, but they always apply to the entire match arm. This restriction could be removed by requiring parentheses in some cases:

```rust
match foo {
    A | B if bar => {}
    // is equivalent to
    (A | B) if bar => {}

    // This could be allowed too:
    (A if bar) | (B if !bar) => {}
    (A if let x = true) | (B if let x = false) => {}
}

```

---

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [January 9, 2021, 5:25pm UTC](https://internals.rust-lang.org/t/the-fall-through-proposal/13792/4 "2021-01-09T17:25:15Z")

</div>

For these examples, yes. Now try it with this:

```rust
pub(crate) enum PatternElement<T: PatternTypes> {
    Arrow,

    Identifier(usize),

    StringKey(usize),
    RegexKey(usize),
    KeySubtree(usize),
    ValueSubtree(usize),
    Paramemter(usize),
    ApplyPredicate(usize),

    SkipStringKey(usize),
    SkipRegexKey(usize),
    SkipKeySubtree(usize),
    SkipValueSubtree(usize),
    SkipParamemter(usize),
    SkipApplyPredicate(usize),

    End
}

```

---

<div class="post-metadata">

**Author:** ![Aloso](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aloso/32/5039_2.png) [@Aloso](https://internals.rust-lang.org/u/Aloso)\
**Post date:** [January 9, 2021, 5:46pm UTC](https://internals.rust-lang.org/t/the-fall-through-proposal/13792/5 "2021-01-09T17:46:46Z")

</div>

I don't follow. What is it that you're trying to achieve, and why is current syntax not good enough?

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [January 9, 2021, 9:56pm UTC](https://internals.rust-lang.org/t/the-fall-through-proposal/13792/6 "2021-01-09T21:56:46Z")

</div>

```rust
match pattern_element {
    case StringKey(key) | SkipStringKey(key) {
        let skip = matches!(pattern_element, SkipStringKey(_));
        // ...
    }
    // etc
}

```

---

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [January 9, 2021, 10:00pm UTC](https://internals.rust-lang.org/t/the-fall-through-proposal/13792/7 "2021-01-09T22:00:23Z")

</div>

Yeah, that's what we're currently using. We believe it has some issues that need to be acknowledged tho, like accidentally using different `pattern_element`s in the `match` and in the `matches!`.

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quinedot/32/7294_2.png) [@quinedot](https://internals.rust-lang.org/u/quinedot)\
**Post date:** [January 9, 2021, 10:02pm UTC](https://internals.rust-lang.org/t/the-fall-through-proposal/13792/8 "2021-01-09T22:02:22Z")

</div>

You could also refactor (or have a translated type) such that you have something like:

```rust
struct Whatever {
  skip: bool,
  whatever: EnumWithoutTheAlmostDuplicateVariants,
}

```

You could also refactor (or have a translated enum) so that your variants look more like:

```rust
pub(crate) enum PatternElement<T: PatternTypes> {
  StringKey(usize, bool),
  // No SkipStringKey
}

```

This one is particular is basically @CAD97's suggestion, factored out into a translation method (`From` impl maybe) so that there's only one place to make those mistakes.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [January 9, 2021, 10:06pm UTC](https://internals.rust-lang.org/t/the-fall-through-proposal/13792/9 "2021-01-09T22:06:55Z")

</div>

A key important thing to note here is that this doesn't have to be the version of the `enum` that you store and pass around. It can be a transient form that you just create temporarily to make it easier to work with.

---

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [January 9, 2021, 10:11pm UTC](https://internals.rust-lang.org/t/the-fall-through-proposal/13792/10 "2021-01-09T22:11:58Z")

</div>

Hmm...

We still gain the memory usage savings when doing that?

* * *

Also: What do we lose from not having a replacement for fall-through? Rust has enums, as a replacement for tagged unions. Rust uh wait did we ever merge/stabilize label-break-value? as an alternative to goto. Having something that's more restrictive than fall-through, but still enables some common fall-through-related ergonomics, would be nice.

---

<div class="post-metadata">

**Author:** ![skysch](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@skysch](https://internals.rust-lang.org/u/skysch)\
**Post date:** [January 10, 2021, 12:52am UTC](https://internals.rust-lang.org/t/the-fall-through-proposal/13792/11 "2021-01-10T00:52:12Z")

</div>

> [@Soni](#):
>
> We believe it has some issues that need to be acknowledged tho, like accidentally using different `pattern_element` s in the `match` and in the `matches!` .

You could solve that by creating an `fn is_skip(&self) -> bool` method and just calling it before (or after) you match. You can always mis-write code. The normal solution to that kind of thing is to separate concerns behind different abstractions.

---

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [January 10, 2021, 2:56am UTC](https://internals.rust-lang.org/t/the-fall-through-proposal/13792/12 "2021-01-10T02:56:15Z")

</div>

Separating concerns behind different abstractions is the problem tho. By keeping things together, you reduce risk of mistakes.

`(Foo if let x=true) | (Bar if let x=false)` would be nice.

---

<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:** [April 10, 2021, 2:56am UTC](https://internals.rust-lang.org/t/the-fall-through-proposal/13792/13 "2021-04-10T02:56:29Z")

</div>

Closed on April 10, 2021, 2:56am UTC

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