我正在try 编写一个与Run关联的方法,该方法将启动一个新线程并在那里继续运行.线程句柄保存在 struct 属性中,但我收到以下错误:
error[E0521]: borrowed data escapes outside of method
--> src\server\dispatcher.rs:33:27
|
28 | pub fn run(&mut self) {
| ---------
| |
| `self` is a reference that is only valid in the method body
| let's call the lifetime of this reference `'1`
...
33 | let main_thread = thread::spawn(move || loop {
| ___________________________^
34 | | match rx.try_recv() {
35 | | Ok(_) | Err(TryRecvError::Disconnected) => {
36 | | break;
... |
43 | | }
44 | | });
| | ^
| | |
| |__________`self` escapes the method body here
| argument requires that `'1` must outlive `'static`
|
我的代码:
pub fn run(&mut self) {
let (tx, rx) = mpsc::channel::<()>();
let packets = Arc::new(Mutex::new((&self.packets)));
let main_thread = thread::spawn(move || loop {
match rx.try_recv() {
Ok(_) | Err(TryRecvError::Disconnected) => {
break;
}
Err(TryRecvError::Empty) => {
let inside = packets.clone();
let qwe = packets.lock().unwrap();
}
}
});
self.main_thread = Some(main_thread);
}