简体   繁体   English

当 Rust 中的 function 需要可变引用时,为什么不可变引用可以匹配?

[英]Why an immutable reference can work in match when mutable reference is required by function in Rust?

Hi I'm currently reading The Rust Programming Language book.嗨,我目前正在阅读Rust 编程语言书。 In error handling chapter I found this code works well:在错误处理章节中,我发现这段代码运行良好:

fn read_username_from_file() -> Result<String, io::Error> {
    let f = File::open("hello.txt");

    let mut f = match f {
        Ok(file) => file,
        Err(e) => return Err(e),
    };

    let mut s = String::new();

    match f.read_to_string(&mut s) {
        Ok(_) => Ok(s),
        Err(e) => Err(e),
    }
}

However, after rewritting using ?但是,改写后使用? operator, the code only works in this way:运算符,代码只能以这种方式工作:

fn read_username_from_file() -> Result<String, io::Error> {
    let mut f = File::open("hello.txt")?;

    let mut s = String::new();

    f.read_to_string(&mut s)?;

    Ok(s)
}

Please note that f now become mutable!请注意f现在变得可变了! In the first version f is immutable.在第一个版本中f是不可变的。 The definition of read_to_string is fn read_to_string(&mut self, buf: &mut String) -> Result<usize> , which requires a mutable self reference. read_to_string的定义是fn read_to_string(&mut self, buf: &mut String) -> Result<usize> ,它需要一个可变的self引用。 But why the first version works without an compile time error here?但是为什么第一个版本在这里没有编译时错误? f ( self ) is not mutable here! f ( self ) 在这里是不可变的!

In the first version, the first f is immutable, but then you shadow it and bind f mutably:在第一个版本中,第一个f是不可变的,但随后您将其遮蔽并可变地绑定f

fn read_username_from_file() -> Result<String, io::Error> {
    let f = File::open("hello.txt"); // this is immutable

    let mut f = match f { ... } // this is mutable
}

The read_to_string function takes a mutable reference to self , so in both examples, f is required to be mutable. read_to_string function 对self进行可变引用,因此在这两个示例中, f都必须是可变的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 Rust 不能在类型构造函数中强制可变引用到不可变引用? - Why Rust can't coerce mutable reference to immutable reference in a type constructor? 为什么 Rust 允许对可变变量的不可变引用? - Why Does Rust Allow an Immutable Reference to a Mutable Variable? 如何使 Rust 可变引用不可变? - How to make a Rust mutable reference immutable? 为什么我不能借用可变引用的不可变引用 - Why I can't borrow immutable reference of a mutable reference 当返回对范围之外的值的可变引用的不可变引用时,为什么在范围结束时丢弃了可变引用? - When an immutable reference to a mutable reference to a value outside the scope is returned, why is the mutable reference dropped when the scope ends? 存在可变引用时传递不可变引用 - Passing an immutable reference when a mutable reference exists Rust:如果不存在其他引用,则允许将不可变引用升级为可变引用 - Rust: allow upgrade of immutable reference to mutable reference, if no other references exist 为什么我不能通过可变值的可变引用的不可变引用来更新值? - Why can't I update a value through an immutable reference of a mutable reference of a mutable value? Rust 将可变枚举引用与向量匹配 - Rust match mutable enum reference with vectors 当我在结构中使用可变引用而不是不可变引用时,为什么会出现生命周期错误? - Why do I get a lifetime error when I use a mutable reference in a struct instead of an immutable reference?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM