Confusing behavior when dropping a value with `_ = value`

⚓ Rust    📅 2025-08-10    👤 surdeus    👁️ 4      

surdeus

Why does the first variant compile and the second one doesn't?

struct NonSend {
    ptr: *mut ffi::c_void,
}

fn take_closure(closure: impl Fn() -> () + Send + 'static) {
    _ = thread::spawn(closure).join();
}

// Compiles!
let non_send = NonSend {
    ptr: ptr::null_mut(),
};
take_closure(move || {
    _ = non_send;
});

// Doesn't!
let non_send = NonSend {
    ptr: ptr::null_mut(),
};
take_closure(move || {
    std::mem::drop(non_send);
});

If I extend the example to print thread id, it seems like non_send is not actually sent to the other thread at all. Why?

1 post - 1 participant

Read full topic

🏷️ Rust_feed