我无法理解有关生命周期的这个错误。 Proxy
是一个结构:
impl Proxy {
pub fn new<A: ip::ToSocketAddr>(address: A) -> Proxy {
Proxy {
address: address.to_socket_addr().unwrap()
}
}
pub fn listen_and_proxy(&self) {
println!("listening {}", self.address);
for stream in TcpListener::bind(self.address).listen().incoming() {
let stream = stream.unwrap();
spawn(move || { // <- this causes an error
let mut stream = stream;
let name = stream.peer_name();
println!("client: {} -> {}", name, self.handle(stream));
});
}
}
}
输出错误:
cannot infer an appropriate lifetime due to conflicting requirements
代码有什么问题?
答案 0 :(得分:3)
您正在尝试调用self.handle(stream)
,这需要将self
移动到新生成的线程中。这是不可能做到的。