# \[Pre-RFC\] Safe goto with value

**URL:** <https://internals.rust-lang.org/t/pre-rfc-safe-goto-with-value/14470>\
**Category:** language design\
**Created:** [April 10, 2021, 9:34pm UTC](https://internals.rust-lang.org/t/pre-rfc-safe-goto-with-value/14470 "2021-04-10T21:34:25Z")\
**Posts on this page:** 1\
**Showing post:** 56

<div class="post-metadata">

**Author:** ![felix.s](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/felix.s/32/8073_2.png) [@felix.s](https://internals.rust-lang.org/u/felix.s)\
**Post date:** [April 17, 2021, 9:08pm UTC](https://internals.rust-lang.org/t/pre-rfc-safe-goto-with-value/14470/56 "2021-04-17T21:08:10Z")

</div>

> [@robinm](#):
>
> Also, if such closure-like syntax would be added to Rust, I think that we should consider if we could re-use them¹ to write closure that interact directly with the control flow of the parent.

I’ll note that I did, at one point, contemplate having something like

```rust
fn foo<T, 'a: break(T)>(val: T) -> ! {
    break 'a val;
}

fn bar() {
    let x = 'a: {
        foo<'a>(5)
    };
    println!("{}", x); // 5
}

```

> [@Aloso](#):
>
> P.S. Perhaps a better name for `arbitrary_control_flow!` would be `state_machine!` .

Which is why I chose `fsm!` for my version of it, as after all one of things it stands for is ‘finite-state machine’. Here’s your example translated:

```rust
let result = fsm! s1(42) {
	s1(n: i32) => {
		if n % 2 == 0 {
			continue s3(n / 2, n + 1)
		}
		s2
	},
	s2 => s1(84),
	s3(x: i32, y: i32) => match foo {
		None => break x + y,
		Some(n) => continue s1(n)
	},
};

```

No new keywords necessary, and it can be easily read from the top, just like a `match` expression. I think it could be implemented on top of `loop` and `match` as a procedural macro. A [simplified version](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7dac0ae649e925ec94e1782cf400f895) (without `continue` and with slightly different initial state syntax) is easily implementable with a pattern-match macro.

(Aside: [the funclets proposal for WebAssembly](https://github.com/WebAssembly/design/issues/1227) seems related, although it is a bit more general: by the look of it, it should be able to express things like binary tree traversal, unlike `fsm!`.)

---

_[View the full topic](https://internals.rust-lang.org/t/pre-rfc-safe-goto-with-value/14470)._
