std-method
fetch_update: conditional update closure
Supply a closure that proposes a new value or returns None to decline. The closure may be evaluated multiple times, so avoid irreversible side effects in it. The result contains the previous value or the last observed value on rejection. Expected stdout (not executed): true true Common mistake: Sending notifications or writing files inside a retryable update closure.
Formal Syntax & Structure
rust syntaxrust
n.fetch_update(set_order, fetch_order, |old| Option<usize>)Working Example
Example Snippetrust
fn main() { use std::sync::atomic::{AtomicUsize, Ordering::Relaxed}; let n = AtomicUsize::new(usize::MAX); let result = n.fetch_update(Relaxed, Relaxed, |old| old.checked_add(1)); println!("{} {}", result.is_err(), n.load(Relaxed) == usize::MAX);}