[rust-dev] Mutable files

Corey Richardson corey at octayn.net
Sun Jul 20 18:34:05 PDT 2014


That's right. `BufferedReader` takes the `Reader` it wraps by-value,
but the `read` method takes `&mut self`. Moving something doesn't
require it to be stored in a mutable variable, but taking a `&mut` to
it does.

On Sun, Jul 20, 2014 at 6:29 PM, David Henningsson <diwic at ubuntu.com> wrote:
> Hi,
>
> Consider these two examples:
>
> 1)
>
> let mut file = File::open(filename);
> file.read(buf);
>
> 2)
>
> let file = File::open(filename);
> let mut reader = BufferedReader::new(file);
> reader.read(buf);
>
> My question is: in example 2, why doesn't BufferedReader need "file" to be
> mutable? After all, BufferedReader ends up calling file.read(), which needs
> a mutable reference to the file.
>
> It looks like I'm able to "bypass" the mutability requirement, just because
> I wrap the file inside a BufferedReader?
>
> // David
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev



-- 
http://octayn.net/


More information about the Rust-dev mailing list