News for hackers. Stuff that matters.
Re:Rust Memory Management by pornel on Tuesday May 09, 2017 @11:58AM (#14301101)
> Wouldn't closing a file handle be an action requiring mutability?

In Rust there is no distinction between mutable owned data and immutable owned data. There are only mutable/immutable references and bindings, but if you own the data, you can mutate it.

   fn foo(v: Vec)
   fn foo(mut v: Vec)
are the same function. `mut` is not part of the type. Even the first function can do `let mut v = v` and make immutable owned `v` a mutable owned one (since Rust can guarantee there are no other references to it, so the mutation is not observable from outside).

It's different from `&mut Vec`, which is a distinct type, and it mutably borrows the Vec.

> If the file handle is just a integer

That's why in Rust such values are wrapped in "newtypes" that don't allow copying, e.g. `struct Handle(i32)`.

> pass by value call to an in-practice pass by reference

Passing ownership of the `Vec` doesn't copy the items.

Ownership/references and passing by value/reference are different concepts.

`Box<Foo>` is an owned value, but it's implemented as a regular pointer (`Foo* ` in C). `&[Foo]` is a reference, but it's actually a small struct passed by value (`struct Ref {size_t len; Foo *data}` in C).

There are no comments on this comment yet.