The error occurs because the type you are trying to send across threads does not implement the Send trait, which is required for thread-safe data transfer. To fix this, ensure all fields within your struct implement Send, or wrap non-Send types like Rc<T> in a Mutex or replace them with thread-safe alternatives like Arc<T>.
use std::sync::{Arc, Mutex};
use std::thread;
struct Data {
// Replace Rc with Arc for thread safety
counter: Arc<Mutex<usize>>,
}
fn main() {
let data = Data {
counter: Arc::new(Mutex::new(0)),
};
let handle = thread::spawn(move || {
let mut num = data.counter.lock().unwrap();
*num += 1;
});
handle.join().unwrap();
}