expression

Transfer callback events to an owning thread

A foreign callback running on another thread must not access unsynchronized Rust state. Transfer owned event data through a thread-safe channel instead. This Rust-thread model does not exercise a foreign callback. Real code must also protect context lifetime and handle backpressure and receiver closure. Expected stdout (not executed): event 6 Common mistake: Sending a borrowed pointer to another thread without proving its lifetime and synchronization.

Formal Syntax & Structure

rust syntaxrust
std::sync::mpsc::Sender<T>

Working Example

Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() {    use std::{sync::mpsc, thread};    let (tx, rx) = mpsc::sync_channel(1);    let worker = thread::spawn(move || tx.send(6_u32).unwrap());    let event = rx.recv().unwrap();    worker.join().unwrap();    println!("event {event}");}
Rust Reference