为什么Option::and_then()不能仅从以下Option::unwrap_or()处理? and_then()仅在Option为Some()时才发生,然后.unwrap_or()仅在Option为None时才发生吗?这是一个代码示例,第一种方法触发借用检查器的投诉,而第二种方法则不会,但是从理论上讲,他们不应该做同样的事情吗?
use std::collections::HashMap;
#[derive(Debug)]
struct Response {
account: String,
status: String,
error: String,
}
fn main() {
let num = String::from("426238");
let record = {
Response {
account: "".into(),
status: "failed".into(),
error: "Invalid Account".into(),
}
};
let mut acct_map = HashMap::new();
acct_map.insert(&num, record);
// Doesn't work
let record = acct_map.remove(&num)
.and_then(|mut r| {r.account = num; Some(r)}) // Should only get processed if there's a Some()
.unwrap_or( // Should only get processed if there's a None
Response {
account: num,
status: String::from("failed"),
error: String::from("The server did not return a response for this account."),
}
); // Yet rustc says variable moved from .and_then()
// Works
let num = String::from("426238");
let record = if let Some(mut response) = acct_map.remove(&num) {
response.account = num;
response
} else {
Response {
account: num,
status: String::from("failed"),
error: String::from("The server did not return a response for this account."),
}
};
}
在尝试前一种方法时收到该投诉后,我改用后者,因为它更易于理解并且可以正常工作,但是我想知道.and_then()和.unwrap_or()背后是否比我的理解还更多。
最佳答案
首先,由于您使用的是unwrap_or
而不是unwrap_or_else
,因此unwrap_or
的参数将始终执行,这意味着它将始终移出num
。
其次,即使您使用了unwrap_or_else
,and_then
或unwrap_or_else
的签名中也没有任何内容告诉借阅检查器这些方法是互斥的,因此在他们看来,两个lambda都可以执行。这是不允许的。if let
是前往此处的方法。
关于rust - 为什么Option <T>::and_then()与后面的.unwrap_or()不互斥?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66643716/