Skip to content
เข้าสู่ระบบ

Channel Structs

MPSC channel types

use std::sync::mpsc::{channel, Sender, Receiver};
use std::thread;
fn main() {
// Create channel
let (tx, rx): (Sender<i32>, Receiver<i32>) = channel();
// Spawn sender thread
thread::spawn(move || {
for i in 1..=5 {
tx.send(i).unwrap();
println!("Sent: {}", i);
}
});
// Receive in main thread
for received in rx {
println!("Received: {}", received);
}
println!("Channel closed");
}

เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม

ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด