[rust-dev] About owned pointer

Niko Matsakis niko at alum.mit.edu
Fri Nov 8 03:33:23 PST 2013

On Fri, Nov 08, 2013 at 07:32:02AM +0800, Liigo Zhuang wrote:
> It's so confusing. If it's not owned box, why not remove ~? Make "str"
> default be dynamic should OK.

I am not sure why Daniel says that a `~str` or `~[]` is not an "owned
box". I guess he's using the term in some specific way. I consider
`~str` and `~[]` to be exactly the same as any other `~T` value in
usage and semantics. They are allocated on the same heap, moved from
place to place, and freed at the same time, etc.

The difference between a type like `str` or `[T]` and other types is
that `str` and `[T]` are actually a series of values: `u8` bytes and
`T` values respectively. This entails a change in representation and
is also the reason that one *must* use a pointer to refer to them,
because the number of values is not known and hence the compiler can't
calculate the size of the value.

Note that we are to some extent evolving how we handle dynamically
sized types like `str` and `[]`. Right now they are quite second class
(you can't impl them etc) but that will change. Nonetheless, it will
remain true that you can never have a *value* of type `str` or `[]`,
but most always use a pointer (either `~[]` or `&[]`).

Also note that a type like `~[T]` is in fact going to be represented
not as a single pointer but rather three pointers, thanks to work by
Daniel in fact.


More information about the Rust-dev mailing list