[−][src]Function replace_with::replace_with
pub fn replace_with<T, D: FnOnce() -> T, F: FnOnce(T) -> T>(
dest: &mut T,
default: D,
f: F
)
Temporarily takes ownership of a value at a mutable location, and replace it with a new value based on the old one.
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.
An important note
On panic (or to be more precise, unwinding) of the closure f, default will be called to
provide a replacement value. default should not panic – doing so will constitute a double
panic and will most likely abort the process.
Example
enum States { A(String), B(String), } impl States { fn poll(&mut self) { replace_with( self, || States::A(String::new()), |self_| match self_ { States::A(a) => States::B(a), States::B(a) => States::A(a), }, ); } }