为什么文件需要可变才能调用Read :: read_to_string? [英] Why does a File need to be mutable to call Read::read_to_string?

查看:110
本文介绍了为什么文件需要可变才能调用Read :: read_to_string?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是第二版Rust教程:

let mut f = File::open(filename).expect("file not found");

我假设文件描述符是一个数字的包装,该数字基本上不会改变并且是只读的.

I'm of the assumption that the file descriptor is a wrapper around a number that basically doesn't change and is read-only.

编译器抱怨文件不能被可变借用,我认为这是因为方法read_to_string将实例作为self自变量,但是问题是为什么" ?文件描述符将发生什么变化?是跟踪光标位置还是其他?

The compiler complains that the file cannot be borrowed mutably, and I'm assuming it's because the method read_to_string takes the instance as the self argument as mutable, but the question is "why"? What is ever going to change about the file descriptor? Is it keeping track of the cursor location or something?

error[E0596]: cannot borrow immutable local variable `fdesc` as mutable
  --> main.rs:13:5
   |
11 |     let fdesc = File::open(fname).expect("file not found");
   |         ----- consider changing this to `mut fdesc`
12 |     let mut fdata = String::new();
13 |     fdesc.read_to_string(&mut fdata)
   |     ^^^^^ cannot borrow mutably

全部来源:

fn main() {
    let args: Vec<String> = env::args().collect();
    let query = &args[1];
    let fname = &args[2];
    println!("Searching for '{}' in file '{}'...", query, fname);

    let fdesc = File::open(fname).expect("file not found"); //not mut
    let mut fdata = String::new();
    fdesc.read_to_string(&mut fdata)
        .expect("something went wrong reading the file");

    println!("Found: \n{}", fdata);
}

推荐答案

我认为这是因为方法read_to_string将实例作为self参数是可变的

I'm assuming it's because the method read_to_string takes the instance as the self argument as mutable

是的,这是正确的:

fn read_to_string(&mut self, buf: &mut String) -> Result<usize>

特征方法Read::read_to_string将接收者作为可变引用,因为通常,这就是实现从某些内容读取"所需的内容.您将要更改缓冲区或偏移量或某物.

The trait method Read::read_to_string takes the receiver as a mutable reference because in general, that's what is needed to implement "reading" from something. You are going to change a buffer or an offset or something.

是的,实际的File 可能仅包含基础文件描述符(例如,在Linux或macOS上)或句柄(例如,Windows).在这些情况下,操作系统将处理跨线程的访问同步.但这甚至不能保证-它取决于平台.像Redox这样的东西实际上可能在其File的实现中具有可变的引用.

Yes, an actual File may simply contain an underlying file descriptor (e.g. on Linux or macOS) or a handle (e.g. Windows). In these cases, the operating system deals with synchronizing the access across threads. That's not even guaranteed though — it depends on the platform. Something like Redox might actually have a mutable reference in its implementation of File.

如果Read特征不接受&mut self,则键入

If the Read trait didn't accept a &mut self, then types like BufReader would have to use things like internal mutability, reducing the usefulness of Rust's references.

另请参阅:

  • Why is it possible to implement Read on an immutable reference to File?

这篇关于为什么文件需要可变才能调用Read :: read_to_string?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆