let (mut tx, mut rx) = channel();でチャンネルを開設したとき、tx, rxの型が何かに苦戦しました。結局、pipe_channel::Sender、pipe_channel::Receiverになるのね。相互にやり取りするのではなく、一方的にデータを送るときなどに有効活用できそうです。
use std::thread; use pipe_channel::*; fn writer(mut tx: pipe_channel::Sender<String>) { let name = "Writer".to_string(); let handle = thread::spawn(move || { println!("{}: Sending rubber duck...", name); tx.send("Rubber duck".to_string()).unwrap(); }); handle.join().unwrap(); } fn reader(mut rx: pipe_channel::Receiver<String>) { let name = "Reader".to_string(); let handle = thread::spawn(move || { println!("{}: Reading...", name); println!("{}: received ({})", name, rx.recv().unwrap()); }); handle.join().unwrap(); } fn main() { let (mut tx, mut rx) = channel(); writer(tx); reader(rx); } fn print_typename<T>(_: T) { println!("{}", std::any::type_name::<T>()); }
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.29s
Running `target/debug/parallel`
Writer: Sending rubber duck…
Reader: Reading…
Reader: received (Rubber duck)