I am leaning rust and using it to write something.
Today, I found that the behavior of the std::fs::File is different from the rust book. Please tell me why it is different.
use std::io::Read;
fn main() {
let mut buf = Vec::new();
let str = "ABC";
let file = std::fs::File::open("path/to/file").unwrap();
let mut mut_file_ref = &file;
mut_file_ref.read_to_end(&mut buf).unwrap();
let string = String::from("");
let mut mut_string_ref = &string;
mut_string_ref.push_str(str);
println!("{} {:?}", file.metadata().unwrap().len(), buf.len());
}
--> src/main.rs:15:5
|
15 | mut_string_ref.push_str(str);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `mut_string_ref` is a `&` reference, so the data it refers to cannot be borrowed as mutable
|
help: consider changing this to be a mutable reference
|
14 | let mut mut_string_ref = &mut string;
| +++