Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Confusing behavior when dropping a value with `_ = value`
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
🏷️ Rust_feed