[rust-dev] Presentation of our vector/function/trait types

Graydon Hoare graydon at mozilla.com
Mon Jul 16 08:34:33 PDT 2012

On 16/07/2012 6:33 AM, Benjamin Striegel wrote:
> This feels simpler, yes. But as long as we're talking about
> presentation, would it be possible to establish standard terminology to
> distinguish between dynamically-sized vectors and fixed-length vectors?
> People will tend to simply refer to both as "vectors" or "vecs", and
> confusion will result. It really doesn't matter what the final terms
> are, as long as they're distinct and easy to type.

Possibly. But I don't think an additional term here will really help; I 
think Patrick's right on the money by comparing this to C and seeking to 
_minimize_ the presentation of variety at work in the model. Consider C 
seriously here. Would you say it has "fixed size" and "dynamic sized" 
array types? I mean, it kinda does: T[10] is a type (fixed) and *T is a 
type (dynamic), but T[] is ... a bit less than a type:

$ cat >t.c
int main() {
   int x[];
$ cc t.c
t.c: In function main:
t.c:2: error: array size missing in x

You can't quite use it in places where a definite amount of allocated 
memory is expected; at least not without "implicitly" giving the size:

$ cat >t.c
int main() {
   int x[] = {1,2,3,4};
$ cc t.c

And in most contexts (assignment to another value, passing to a 
function) it tends to drop the definite size and decay to a pointer, 
albeit a "narrow" one without bounds checks. Yet users mostly come to 
terms with this: once they have a mental model of which allocations are 
contained in others and which are pointers, the rules of the array types 
become (mostly) obvious consequences.

I'd say we're following pretty much the same model, except we're 
maintaining the difference between a vec-pointer and an element-pointer, 
refusing to decay between the two, and the vec-pointer carries a bound 
so it can be bounds-checked. This is healthy! Randomly indexing 
element-pointers and hoping you stay in-bounds in C is the main source 
of memory errors there :)

Our vectors aren't really "fixed-size" or "dynamic-size"; just the 
static types. All vectors are _some_ size, it's just that in some 
contexts (those where the vec is stored in the interior of some other 
allocation, including a stack frame) the user has to specify the size 
statically as part of a variable's type; whereas in others (heap 
allocations, allocations held in other frames and passed by pointer, 
etc.) the variable is a pointer so it doesn't have to statically encode 
the size of its pointee: all pointers are the same size (1 or 2 words, 
depending on whether bounds-checked) so allocating the variable is just 
allocating a pointer.


More information about the Rust-dev mailing list