std-method

Atomic fetch_min and fetch_max

Atomically replace a value by its minimum or maximum with a candidate. Use it for independent extrema, not a coupled record with several fields. Expected stdout (not executed): 8 Common mistake: Assuming the recorded peak and a separately stored timestamp are updated together.

Formal Syntax & Structure

rust syntaxrust
n.fetch_min(candidate, order); n.fetch_max(candidate, order)

Working Example

Example Snippetrust
fn main() {    use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};    let peak = AtomicUsize::new(4);    for sample in [2, 8, 3] { peak.fetch_max(sample, Relaxed); }    println!("{}", peak.load(Relaxed));}
Rust Reference