use std::{
sync::{mpsc, Arc, Mutex},
thread,
};
struct Worker {
id: usize,
thread: thread::JoinHandle<()>,
}
impl Worker {
fn new(
id: usize,
receiver: Arc<Mutex<mpsc::Receiver<Box<dyn FnOnce() + Send + 'static>>>>,
) -> Worker {
let thread = thread::spawn(move || loop {
let job = receiver.lock().unwrap().recv().unwrap();
println!("Worker {} got a job; executing.", id);
(*job)();
});
Worker { id, thread }
}
}
无法编译:
error[E0161]: cannot move a value of type dyn std::ops::FnOnce() + std::marker::Send: the size of dyn std::ops::FnOnce() + std::marker::Send cannot be statically determined
--> src/lib.rs:21:13
|
21 | (*job)();
| ^^^^^^
这是书中的错误还是我错过了什么?
答案 0 :(得分:4)
似乎您指的是本书中紧随其后的部分:
理论上,此代码应编译。不幸的是,锈 编译器还不够完善,我们会收到此错误:
error[E0161]: cannot move a value of type std::ops::FnOnce() + std::marker::Send: the size of std::ops::FnOnce() + std::marker::Send cannot be statically determined --> src/lib.rs:63:17 | 63 | (*job)(); | ^^^^^^
因此,不是不是书中的错误;他们故意将其包括在内以显示问题。请继续阅读本章,以了解他们建议如何解决。
另请参阅: