产生任务时的生命周期错误

时间:2014-12-23 11:03:00

标签: rust lifetime

我无法理解有关生命周期的这个错误。 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

代码有什么问题?

1 个答案:

答案 0 :(得分:3)

您正在尝试调用self.handle(stream),这需要将self移动到新生成的线程中。这是不可能做到的。