如果你查看docs for Read
,大多数方法都接受&mut self
.这是有道理的,因为从某物读取通常会更新内部偏移量,所以下一次读取会返回不同的数据.然而,这包括:
use std::io::Read;
use std::fs::File;
fn main() {
let file = File::open("/etc/hosts").unwrap();
let vec = &mut Vec::new();
(&file).read_to_end(vec).unwrap();
println!("{:?}", vec);
}
文件是不可变的,但数据肯定正在被读取.我觉得这似乎不正确.It was pointed out that there is an impl<'a> Read for &'a File
,但一个不可变的实例似乎正在发生变异这一事实似乎仍然很奇怪.