# Branching or Exit Points : Quad Saga

**URL:** <https://internals.rust-lang.org/t/branching-or-exit-points-quad-saga/5862>\
**Category:** language design\
**Created:** [September 5, 2017, 3:03pm UTC](https://internals.rust-lang.org/t/branching-or-exit-points-quad-saga/5862 "2017-09-05T15:03:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![genieme](https://avatars.discourse-cdn.com/v4/letter/g/50afbb/32.png) [@genieme](https://internals.rust-lang.org/u/genieme)\
**Post date:** [September 5, 2017, 3:03pm UTC](https://internals.rust-lang.org/t/branching-or-exit-points-quad-saga/5862/1 "2017-09-05T15:03:23Z")

</div>

Branching or Exit Points : Quad Saga

- Consistency : promotes simplicity
- Simplicity : promotes easy learning.

Names for the 4 functions :

- fn branchOut() returns ()
- fn branchTo(Label) returns ()
- fn branchWith() returns Val
- fn branchIf(BoolExp) returns ()

Syntaxes of the 4 Expressions :

- break; or break ();
- break 'label;
- break val;
- break if x \> y; ?

we need ‘break if’ expression.

‘if’ is the filter for the exit point : exit iff true.

Usage : inside ‘loop’ block

loop {

break if x \> y;

// equivalent to js : while (x \> y) {… }

… }

loop { …

// equivalent to js : do{ … } while (x \> y);

break if x \> y; }

loop { …

// see ada-lang : ‘exit when’ syntax for an equivalent. // 2 or more can be placed anywhere in the the loop, scattered or not.

break if x \> y;

… }

Note : that is a lot of flexibility and expressive powess. Yet the rust core team keeps it from us ?? 🙂

Please correct the wrong ?? 🙂

when teaching the branching concept to school students, leaving one component out is a disservice. they need complete understanding and practice of the concept.

Please Help Us Out. THANKS.

---

<div class="post-metadata">

**Author:** ![dtolnay](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dtolnay/32/1447_2.png) [@dtolnay](https://internals.rust-lang.org/u/dtolnay)\
**Post date:** [September 5, 2017, 3:19pm UTC](https://internals.rust-lang.org/t/branching-or-exit-points-quad-saga/5862/2 "2017-09-05T15:19:37Z")

</div>

What are some reasons to prefer `break if x > y` over `if x > y { break }`?

---

<div class="post-metadata">

**Author:** ![genieme](https://avatars.discourse-cdn.com/v4/letter/g/50afbb/32.png) [@genieme](https://internals.rust-lang.org/u/genieme)\
**Post date:** [September 5, 2017, 3:57pm UTC](https://internals.rust-lang.org/t/branching-or-exit-points-quad-saga/5862/3 "2017-09-05T15:57:27Z")

</div>

@dtolnay the ‘if’ keyword is being used as a filter.

we can get rid of if statement or expression, if need be

we can get rid of while(){} statement or expression also

I am trying to promote consistency, which may lower the learning

curve barrier: ‘On Boarding’.

And use match or loop expression or statement instead.

less syntax -\> less to learn

---

<div class="post-metadata">

**Author:** ![Diggsey](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/diggsey/32/6311_2.png) [@Diggsey](https://internals.rust-lang.org/u/Diggsey)\
**Post date:** [September 5, 2017, 4:09pm UTC](https://internals.rust-lang.org/t/branching-or-exit-points-quad-saga/5862/4 "2017-09-05T16:09:29Z")

</div>

> the ‘if’ keyword is being used as a filter.

that is a very arbitrary distinction to make, there's no real difference between the two constructs - you seem to be suggesting that the design of the AST is important for a beginner to the language. Are you sure you're not just arguing for this syntax because it reads more like natural language?

> less syntax -\> less to learn

So how does adding _more_ redundant syntax help?

---

<div class="post-metadata">

**Author:** ![dtolnay](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dtolnay/32/1447_2.png) [@dtolnay](https://internals.rust-lang.org/u/dtolnay)\
**Post date:** [September 5, 2017, 4:10pm UTC](https://internals.rust-lang.org/t/branching-or-exit-points-quad-saga/5862/5 "2017-09-05T16:10:16Z")

</div>

To clarify, in your code is the behavior identical to `if x > y { break }`?

---

<div class="post-metadata">

**Author:** ![genieme](https://avatars.discourse-cdn.com/v4/letter/g/50afbb/32.png) [@genieme](https://internals.rust-lang.org/u/genieme)\
**Post date:** [September 5, 2017, 4:41pm UTC](https://internals.rust-lang.org/t/branching-or-exit-points-quad-saga/5862/6 "2017-09-05T16:41:14Z")

</div>

@dtolnay and @Diggsey

I am discussing the break exit points as a set of four entities.

they are all related to the same concept : exit points.

that is the consistency.

‘break if’ is the last one remaining.

Yes its semaintics is equivalent to : if (x\>y) {break;}

but that is not an exit-point or branching.

- break;
- break 5;
- break 'label;
- break if x \> y;

CONSISTENCY 🙂

I personally do not use the while keyword.

it is conditional repetition. and not that flexible at all.

‘loop’ keyword + ‘break if’ combine give great flexibility.

see the many ways of using them in the original post.

we can deprecate ‘while’ and ‘if’ statement altogether.

one syntax in : two syntax out

---

<div class="post-metadata">

**Author:** ![ExpHP](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/exphp/32/6208_2.png) [@ExpHP](https://internals.rust-lang.org/u/ExpHP)\
**Post date:** [September 5, 2017, 8:17pm UTC](https://internals.rust-lang.org/t/branching-or-exit-points-quad-saga/5862/7 "2017-09-05T20:17:01Z")

</div>

> we can deprecate ‘while’ and ‘if’ statement altogether.

While we're at it, we can deprecate `for` loops and block expressions too. And we can also get rid of bare `break`; best be explicit with `break if true`:

```rust
// old rust
let sorted = {
    let mut vec: Vec<_> = iter.collect();
    vec.sort();
    vec
};

// new, idiomatic rust
let sorted = loop {
    let mut vec: Vec<_> = iter.collect();
    vec.sort();
    break vec if true;
};

```

---

<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:28am UTC](https://internals.rust-lang.org/t/branching-or-exit-points-quad-saga/5862/8 "2019-03-25T08:28:58Z")

</div>

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