If you check out the docs for Read
, most of the methods accept a &mut self
. This makes sense, as reading from something usually updates an internal offset so the next read returns different data. However, this compiles:
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);
}
The file isn't mutable, but the data is certainly being read in. This seems incorrect to me. It was pointed out that there is an impl<'a> Read for &'a File
, but the fact that an immutable instance is seemingly being mutated still seems odd.