Because Foo is a POD type (implements the Copy trait). Essentially, types that 
can be copied by copying bits only (not allocating) are POD types, and all 
others move.

This may be changed with the Opt-In Built-in Traits proposal so that POD types 
must be specially declared to implement Copy before they will copy.

Patrick

On July 20, 2014 7:39:35 PM PDT, David Henningsson <di...@ubuntu.com> wrote:
>
>
>On 2014-07-21 03:33, Patrick Walton wrote:
>> On 7/20/14 6:29 PM, David Henningsson 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?
>>
>> Because `BufferedReader::new` moves `file` and takes ownership of it.
>> (You can see this if you try to use `file` again: the compiler will
>> prevent you.) Mutability is inherited through ownership in Rust: that
>> is, the current owner determines the mutability of a piece of data.
>So,
>> the mutability of `reader` determines the mutability of the `File`
>> object at the time you try to read, and the mutability restriction is
>> satisfied.
>
>Thanks for the quick answer!
>
>I did two more examples to try to understand when things are moved:
>
>3)
>struct Dummy {
>   foo: int,
>   bar: int
>}
>
>let f = Dummy {foo: 10, bar: 5};
>let mut g = f; // Here the assignment copies..?
>println!("{}", f.foo + g.foo); // Ok
>
>4)
>
>let f = File::open(filename);
>let mut g = f; // Here the assignment moves..?
>f.tell(); // Fails - use of moved value
>
>How come that the assignment moves in example 4), and copies in example
>3)?
>
>// David

-- 
Sent from my Android phone with K-9 Mail. Please excuse my brevity.
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to