选项<Receiver> 在上一个循环迭代中移动

Option<Receiver> Moved in Previous Loop Iteration

我正在生成一个可以完成一些工作的线程。有时我希望这个线程在工作完成后结束,有时我希望它等待更多工作完成。为此,我传入一个 Option<Receiver<T>>。如果 Option<Receiver<T>>None 线程应该终止,否则它应该等待接收更多工作。

fn foo(rx: Option<Receiver<usize>>) {
    thread::spawn(move || {
        loop {
            do_some_work();
            if let Some(r) = rx {
                match r.recv() {
                    Ok(x)  => {}
                    Err(_) => panic!("Oh no!"),
                }
            } else {
                break; //Die
            }
        }
    });
}

(link to playground)

编译器说:

error[E0382]: use of moved value
  --> src/lib.rs:10:25
   |
10 |             if let Some(r) = rx {
   |                         ^ value moved here, in previous iteration of loop
   |
   = note: move occurs because value has type `std::sync::mpsc::Receiver<usize>`, which does not implement the `Copy` trait

但是,如果 Receiver 没有包含在 Option 中,一切都很好。

fn foo(rx: Receiver<usize>) {
    thread::spawn(move || {
        loop {
            do_some_work();
            match rx.recv() {
                Ok(x)  => {}
                Err(_) => panic!("Oh no!"),
            }
        }
    });
}

当您写入 if let Some(r) = rx 时,您会消耗 rx,使其以后不可用。

您可以使用 as_ref() 来获取对内部对象的引用,使 rx 可用:

fn foo(rx: Option<Receiver<usize>>) {
    thread::spawn(move || {
        loop {
            do_some_work();
            if let Some(r) = rx.as_ref() {
                match r.recv() {
                    Ok(x) => {}
                    Err(_) => panic!("Oh no!"),
                }
            } else {
                break; //Die
            }
        }
    });
}

(link to playground)