# Idea: continue-to-else

**URL:** <https://internals.rust-lang.org/t/idea-continue-to-else/9036>\
**Category:** Uncategorized\
**Created:** [December 15, 2018, 3:36am UTC](https://internals.rust-lang.org/t/idea-continue-to-else/9036 "2018-12-15T03:36:06Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![crlf0710](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/crlf0710/32/1987_2.png) [@crlf0710](https://internals.rust-lang.org/u/crlf0710)\
**Post date:** [December 15, 2018, 3:36am UTC](https://internals.rust-lang.org/t/idea-continue-to-else/9036/1 "2018-12-15T03:36:06Z")

</div>

I’m imagining this syntax sugar: allow using labelled-continue within if when `else` if labelled.

```rust
if xxxx {
     if yyy {
       //do something
     } else {
        continue 'fallback;
     }
}
'fallback: else {
    //do something
}

```

Just an idea. Wonder if people think this would be useful?

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [December 15, 2018, 4:56am UTC](https://internals.rust-lang.org/t/idea-continue-to-else/9036/2 "2018-12-15T04:56:24Z")

</div>

This seems too much like goto, so I don’t like it.

* * *

edit:  
I should clarify, this is too much like unstructured goto

---

<div class="post-metadata">

**Author:** ![rpjohnst](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rpjohnst/32/9524_2.png) [@rpjohnst](https://internals.rust-lang.org/u/rpjohnst)\
**Post date:** [December 15, 2018, 5:10am UTC](https://internals.rust-lang.org/t/idea-continue-to-else/9036/3 "2018-12-15T05:10:56Z")

</div>

It’s probably more straightforward just to move the computation of `yyy` earlier so you can include it with the `if xxxx`.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [December 15, 2018, 5:20am UTC](https://internals.rust-lang.org/t/idea-continue-to-else/9036/4 "2018-12-15T05:20:50Z")

</div>

Rust has three keywords for structured goto (`return`, `break`, and `continue`), so just being _like_ `goto` isn’t enough on its own. The question is whether it’s the one “good” kind of goto that we already have ([https://github.com/rust-lang/rfcs/pull/2046#issuecomment-311230800](https://github.com/rust-lang/rfcs/pull/2046#issuecomment-311230800)).

This is, however, not that good kind of goto. It’s going into a sibling block, not leaving a block it’s already in.

So you probably want to use

> <https://github.com/rust-lang/rust/issues/48594>

And rewrite it something like this:

```rust
'whole: {
    if xxxx {
        if yyy {
            //do something
            break 'whole;
        }
    }

    //do something
}

```

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [December 15, 2018, 8:48am UTC](https://internals.rust-lang.org/t/idea-continue-to-else/9036/5 "2018-12-15T08:48:24Z")

</div>

The simple question is: why would you want this? I mean, genuinely… you can imagine _arbitrarily_ complex primitives for control flow that would exactly fit some very specific use-case in some concocted situation. That’s not a good approach to language design, though.

In particular, your code, which looks like this, with a bit more descriptive labels:

```rust
if outer_condition {
     if inner_condition {
       do_something();
     } else {
        continue 'fallback;
     }
}
'fallback: else {
    do_something_else();
}

```

can be trivially rewritten as:

```rust
if outer_condition && inner_condition {
    do_something();
} else if !outer_condition || outer_condition && !inner_condition {
    do_something_else();
}

```

which in turn is just the same as

```rust
if outer_condition && inner_condition {
    do_something();
} else {
    do_something_else();
}

```

I fail to see how the proposed syntax could have any advantage: it merely makes interpreting control flow _more difficult_ without adding any new functionality.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [December 15, 2018, 9:52am UTC](https://internals.rust-lang.org/t/idea-continue-to-else/9036/6 "2018-12-15T09:52:27Z")

</div>

> [@H2CO3](#):
>
> can be trivially rewritten as

I think it's fair to assume the real case is slightly more complicated, such that it's not as obvious. For example, tweak it slightly to

```rust
if outer_condition {
    let x = expensive();
    if inner_condition(&x) {
        do_something(x);
    } else {
        continue 'fallback;
    }
}
'fallback: else {
    do_something_else();
}

```

which doesn't translate to a "trivial" if-else, but does work cleanly with LBV:

```rust
'whole: {
    if outer_condition {
        let x = expensive();
        if inner_condition(&x) {
            do_something(x);
            break 'whole;
        }
    }
    
    do_something_else();
}

```

**EDIT** : I feel like the post below this completely missed my point -- I could copy-paste the same "I don't care about any particular concrete example; you can make it slightly more complete and that rewrite doesn't work" reply to it -- but I'll let the thread die by not making a new reply for that.

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [December 15, 2018, 9:58am UTC](https://internals.rust-lang.org/t/idea-continue-to-else/9036/7 "2018-12-15T09:58:35Z")

</div>

With help from RFC 2497, you could write that as:

```rust
if outer_condition
    && let x = expensive()
    && inner_condition(&x) {
        do_something(x);
} else {
    do_something_else();
}

```

---

<div class="post-metadata">

**Author:** ![crlf0710](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/crlf0710/32/1987_2.png) [@crlf0710](https://internals.rust-lang.org/u/crlf0710)\
**Post date:** [December 15, 2018, 12:32pm UTC](https://internals.rust-lang.org/t/idea-continue-to-else/9036/8 "2018-12-15T12:32:58Z")

</div>

Ok, i’m persuaded. Thank you everybody 🙂

---

<div class="post-metadata">

**Author:** ![dhm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dhm/32/4879_2.png) [@dhm](https://internals.rust-lang.org/u/dhm)\
**Post date:** [December 22, 2018, 1:34am UTC](https://internals.rust-lang.org/t/idea-continue-to-else/9036/9 "2018-12-22T01:34:41Z")

</div>

What about:

```rust
if outer_condition && {
    let x = expensive();
    if inner_condition(&x) {
        do_something(x);
        true
    } else { false }
}
{}
else {
    do_something_else()
}

```

EDIT: or even shorter (but maybe less readable)

```rust
if !(outer_condition && {
    let x = expensive();
    inner_condition(&x) && {
        do_something(x);
        true
    }
})
{
    do_something_else()
}

```

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [December 22, 2018, 3:58am UTC](https://internals.rust-lang.org/t/idea-continue-to-else/9036/10 "2018-12-22T03:58:20Z")

</div>

That would also work

---

<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:** [March 25, 2019, 8:31am UTC](https://internals.rust-lang.org/t/idea-continue-to-else/9036/11 "2019-03-25T08:31:18Z")

</div>

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