[][src]Function continuation::call_with_escape_continuation

pub fn call_with_escape_continuation<T, E, Body>(body: Body) -> Result<T, E> where
    Body: FnOnce(&mut dyn FnMut(E) -> !) -> T, 

Mimic C++'s exceptions locally.

Invokes body with one argument, an escape continuation.

If body returns normally, call_with_escape_continuation returns that value wrapped in Ok.

The escape continuation is a function of one argument which never returns. If the escape continuation is invoked, call_with_escape_continuation will return its argument wrapped in Err.

For example, the following expression returns Ok(10):

call_with_escape_continuation(
  |throw| if true { 5 + 5 } else { throw("unreachable") },
)

Whereas this expression returns Err(10):

call_with_escape_continuation(
  |throw| if false { "unreachable" } else { throw(20 - 10) },
)

By convention, the escape continuation should be named throw, or some variation thereof. Contexts with multiple nested call_with_escape_continuation should each name their escape continuations throw_foo, where foo describes the exceptional situation; e.g. throw_io_error, throw_thread_panicked, etc.