admin管理员组

文章数量:1125590

Why unsafe block is necessary even if the static mutable variable is of Atomic type.

fn print_id() {
    use std::sync::atomic::{AtomicU64, Ordering};
    
    static mut ID: AtomicU64 = AtomicU64::new(1);
    unsafe {
        println!("{}", ID.fetch_add(1, Ordering::SeqCst));
    }
}

fn main() {
    print_id();
    print_id();
    print_id();
    print_id();
    print_id();
}

本文标签: rustunsafe block for static mutable atomic typeStack Overflow