[−][src]Function replace_with::replace_with_or_abort_and_return
pub fn replace_with_or_abort_and_return<T, U, F: FnOnce(T) -> (U, T)>(
dest: &mut T,
f: F
) -> U
Temporarily takes ownership of a value at a mutable location, and replace it with a new value based on the old one. Aborts on panic. Lets the closure return a custom value as well.
We move out of the reference temporarily, to apply a closure f
, returning a new value, which
is then placed at the original value's location.
This is effectively the same function as replace_with_or_abort
, but it lets the closure
return a custom value as well.
An important note
On panic (or to be more precise, unwinding) of the closure f
, the process will abort to
avoid returning control while dest
is in a potentially invalid state.
If this behaviour is undesirable, use replace_with_and_return
or
replace_with_or_default_and_return
instead.
Equivalent to replace_with_and_return(dest, || process::abort(), f)
.
Example
fn take<T>(option: &mut Option<T>) -> Option<T> { replace_with_or_abort_and_return(option, |option| (option, None)) }