我只想tokio::net::TcpStream.split
在结构中使用方法并将其保留为字段变量,但出现错误error[E0597]: 'stream' does not live long enough
。当我试图将借来的值保存到 struct 的字段(如Struct std::path::Path
. 我知道Path
问题会通过 using 得到解决PathBuf
,但这次我不确定。你能给我一个建议让它工作吗?
use tokio::net::TcpStream;
use tokio::net::tcp::{ReadHalf, WriteHalf};
struct TT<'a>{
pub reader: Option<ReadHalf<'a>>,
pub writer: Option<WriteHalf<'a>>,
}
impl<'a> TT<'a> {
fn set_reader_and_writer(&mut self, mut stream: TcpStream) {
let (reader, writer) = stream.split();
self.reader = Some(reader);
self.writer = Some(writer);
}
}
$ cargo build [master|…4]
Blocking waiting for file lock on build directory
Compiling tcpst v0.1.0 (/tmp/tcpst)
error[E0597]: `stream` does not live long enough
--> src/main.rs:11:32
|
9 | impl<'a> TT<'a> {
| -- lifetime `'a` defined here
10 | fn set_reader_and_writer(&mut self, mut stream: TcpStream) {
11 | let (reader, writer) = stream.split();
| ^^^^^^ borrowed value does not live long enough
12 | self.reader = Some(reader);
| -------------------------- assignment requires that `stream` is borrowed for `'a`
13 | self.writer = Some(writer);
14 | }
| - `stream` dropped here while still borrowed
error: aborting due to previous error
For more information about this error, try `rustc --explain E0597`.
error: could not compile `tcpst`.