在尝试使用structopt访问命令行参数,然后将其传递以进行进一步处理时,我试图弄清楚如何解决此问题:
fn main() {
let opt = Opt::from_args();
for _ in 0..opt.threads {
GLOBAL_THREAD_COUNT.fetch_add(1, Ordering::SeqCst);
thread::spawn(move || {
// We need to catch panics to reliably signal exit of a thread
let result = panic::catch_unwind(move || {
// do some work
worker(opt.iterations);
});
// process errors
match result {
_ => {}
}
// signal thread exit
GLOBAL_THREAD_COUNT.fetch_sub(1, Ordering::SeqCst);
});
}
// Wait for other threads to finish.
while GLOBAL_THREAD_COUNT.load(Ordering::SeqCst) != 0 {
thread::sleep(Duration::from_millis(1));
}
}
错误:
error[E0382]: use of moved value: `opt`
--> src/main.rs:80:23
|
74 | let opt = Opt::from_args();
| --- move occurs because `opt` has type `Opt`, which does not implement the `Copy` trait
...
80 | thread::spawn(move|| {
| ^^^^^^ value moved into closure here, in previous iteration of loop
...
84 | worker(opt.iterations); // TODO: make configurable (in database)
| --- use occurs due to use in closure
我知道所有权会引起问题,但是使用&opt.iterations
并没有帮助,这基本上是我(有限的)防锈知识的终结。
我需要将该参数传递给实际的worker函数。