Marker Traits
Send, Sync, Sized, Unpin และ marker traits อื่นๆ
Send Trait
Section titled “Send Trait”use std::thread;use std::sync::Arc;use std::rc::Rc;
fn main() { // Send - safe to transfer between threads
// Most types are Send let data = vec![1, 2, 3]; thread::spawn(move || { println!("Vec in thread: {:?}", data); }).join().unwrap();
// Arc is Send (when T: Send + Sync) let arc = Arc::new(42); let arc_clone = Arc::clone(&arc); thread::spawn(move || { println!("Arc in thread: {}", arc_clone); }).join().unwrap();
// Rc is NOT Send let rc = Rc::new(42); // thread::spawn(move || { println!("{}", rc); }); // Error! println!("Rc stays in main: {}", rc);
println!("\n=== Send Types ==="); println!("- Primitive types (i32, bool, etc.)"); println!("- String, Vec<T> where T: Send"); println!("- Arc<T> where T: Send + Sync"); println!("- Mutex<T> where T: Send");
println!("\n=== NOT Send ==="); println!("- Rc<T> (non-atomic refcount)"); println!("- Raw pointers (*const T, *mut T)");}เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google