[][src]Function continuation::call_with_repeat_continuation

pub fn call_with_repeat_continuation<Return, Payload, Body>(
    initial_payload: Payload,
    body: Body
) -> Return where
    Body: FnMut(Payload, &mut dyn FnMut(Payload) -> !) -> Return, 

Mimic the behavior of c's setjmp/longjmp.

Invokes body with two arguments: a payload and a repeat-continuation.

If body returns normally, call_with_repeat_continuation returns its value.

The repeat continuation is a function of one argument which never returns. When invoked, the repeat continuation causes body to be re-invoked using the repeat continuation's argument as a new payload.

For example, the following will print the numbers 1..=10, then return 10:

call_with_repeat_continuation(
  0,
  |i, repeat| {
    println!("{}", i);
    if i == 10 { i } else { repeat(i + 1) }
  },
)

No convention is imposed as to the name of the repeat continuation.