[rust-dev] Managed & owned boxes in Rust

Patrick Walton pwalton at mozilla.com
Wed Dec 5 18:24:01 PST 2012


On 12/5/12 6:07 PM, Niko Matsakis wrote:
> Great article.  Regarding why ~[T] and ~T behave somewhat differently,
> the reason is that the size of [T] is not known to the compiler!  It's
> kind of like how in C, if you have T[3], that's a perfectly fine type,
> but T[] is sort of a degenerate type that gets converted to T*
> willy-nilly.  The reason is that the compiler can't really manipulate an
> "unknown number of T's in a row", which is what [T] (Rust) and T[] (C)
> represent.  As an example, it can't put such a value on the stack, since
> it doesn't know how much stack space to allocate (well, it can't put
> that on the stack without using alloca() or something similar).

We have talked about special-casing "&*" in the grammar to mean "borrow" 
regardless of the type of pointer, and we could also conceivably 
special-case "@*" and "~*", which would make what you wrote work. 
Basically you would be permitted to dereference 
&[T]/@[T]/~[T]/&str/@str/~str for the sole purpose of borrowing it or 
copying its contents into another heap.

Patrick



More information about the Rust-dev mailing list