Hi,
I have the following code and I simply read a file into a buffer. I use
std::io::BufferRead for the same. BufferRead implements BufRead and Read trait.
One of the methods in the trait definition is read and it is declared as
fn read(&mut self, buf: &mut [u8]) -> Result
The method is invoked through BufReader instance. ( I think instance is better
word than object in Rust .. is it not ? )
My question is why it has to mut ? as its going to be a read only instance.
Thanks,
S.Gopinath
use std::io::BufReader ;
use std::fs::File;
use std::io::prelude::*;
fn main()
{
let fd1 = File::open("testfile").expect("File opening error");
let mut reader = BufReader::new(& fd1); // Why this has to be mut ?
//Why read takes mutable reference
let mut buffer : Vec<u8> = vec![0;1000000];
//let seek_pointer : u64 =0 ;
//seek_pointer = fd1.seek(SeekFrom::Start(1000000)).expect("Seek Error");
let n = reader.read(&mut buffer).expect("Error in reading");
}
```