rust - 为什么Option <T>::and_then()与后面的.unwrap_or()不互斥?

标签 rust option

为什么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_elseand_thenunwrap_or_else的签名中也没有任何内容告诉借阅检查器这些方法是互斥的,因此在他们看来,两个lambda都可以执行。这是不允许的。if let是前往此处的方法。

关于rust - 为什么Option <T>::and_then()与后面的.unwrap_or()不互斥?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66643716/

相关文章:

generics - Rust 常量表达式依赖于泛型参数

javascript - JQuery 从列表中删除选择选项

javascript - .text函数不起作用

struct - '&self' 和 '&' a self' 有什么区别?

rust - 为什么没有为明确实现的类型实现特征?

rust - 为什么二进制 + 运算符不能与两个 &mut int 一起使用?

ruby-on-rails - 如何从 Rails 4.0.2 Controller 中的选择标签中获取所选选项

option - 使用 YQL 获取金融期权数据

cookies - 禁用谷歌加一键设置的cookies

rust - 如何使用 Cargo 运行项目示例?