[英]Why does the multithreaded web server example from the Rust book not compile?
This is a code sample from the book : 这是本书中的代码示例 :
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 }
}
}
It doesn't compile: 它不会编译:
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)();
| ^^^^^^
Is this a bug in the book or am I missing something? 这是书中的错误还是我遗漏了一些东西?
It seems that you are referring to the section of the book that is immediately followed by the text: 似乎您指的是本书中紧随其后的部分:
Theoretically, this code should compile. 从理论上讲,此代码应编译。 Unfortunately, the Rust compiler isn't perfect yet, and we get this error: 不幸的是,Rust编译器还不是完美的,我们得到这个错误:
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)(); | ^^^^^^
Thus no, it's not a bug in the book; 因此,不是,这不是书中的错误。 they deliberately included that to show a problem. 他们故意将其包括在内以显示问题。 Please continue reading the chapter to see how they suggest addressing it. 请继续阅读本章,以了解他们建议如何解决。
See also: 也可以看看:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.