【发布时间】:2021-07-18 18:39:23
【问题描述】:
我有一个函数只能返回一个错误,所以我想创建一次并在需要时返回该实例。
这里有一个例子来演示:
fn foo() -> Result<(), String> {
let err = String::from("error message");
let a = function_that_returns_an_option().ok_or_else(|| err)?;
let b = function_that_returns_another_option().ok_or_else(|| err)?;
// ...do something with a and b...
Ok(())
}
这会导致以下编译错误:
error[E0382]: use of moved value: `err`
--> src/main.rs:12:61
|
10 | let err = String::from("error message");
| --- move occurs because `err` has type `String`, which does not implement the `Copy` trait
11 | let a = function_that_returns_an_option().ok_or_else(|| err)?;
| -- --- variable moved due to use in closure
| |
| value moved into closure here
12 | let b = function_that_returns_another_option().ok_or_else(|| err)?;
| ^^ --- use occurs due to use in closure
| |
| value used here after move
我认为移动是懒惰的,但如果我理解正确,err 即使没有执行闭包也会移动到第一个闭包,因此它不能在第二个闭包中使用(或者编译器不知道并拒绝它只是为了安全?)。我可以用err.clone() 替换闭包中的err,但这有点违背了目的。是否有另一种更“惯用”的方式来做到这一点?
【问题讨论】: