[rust-dev] Vectors, mutability & vec::dedup

Patrick Walton pwalton at mozilla.com
Thu Dec 13 08:48:59 PST 2012

On 12/13/12 12:17 AM, Tom Lee wrote:
> It looks like the 'move' operations here are directly modifying the
> contents of the vector by moving data around in the vector. This
> strikes me as kind of strange. I've allocated an immutable vector on
> the exchange stack and so make the assumption that the contents of the
> vector is in effect fixed. Is that assumption cast to the wind as soon
> as I start working with a mutable, borrowed pointer to that same
> vector?

Mutability inherits through ownership in Rust. This line actually 
declares a mutable vector:

     let mut values = ~["foo", "bar", "foo", "baz"];

For example, this will compile:

     values[0] = ~"widget";

The reason why it works this way is so that you can transform values 
from mutable to immutable and vice versa by simply moving them to a 
mutable or immutable location. For instance, you could build up a 
hashtable by mutating it, then place it in an immutable ARC box for many 
tasks to access in parallel.


More information about the Rust-dev mailing list