1
Fork 0

Fix vector/array/slice terminology in manual.

Fixes #16015.
This commit is contained in:
Steve Klabnik 2014-08-28 14:29:48 -04:00
parent b5165321e4
commit a021330b1d

View file

@ -3564,34 +3564,36 @@ let (a, b) = p;
assert!(b != "world"); assert!(b != "world");
~~~~ ~~~~
### Vector types ### Vector, Array, and Slice types
The vector type constructor represents a homogeneous array of values of a given type. Rust has three different types for a list of items:
A vector has a fixed size.
(Operations like `vec.push` operate solely on owned vectors.)
A vector type can be annotated with a _definite_ size, such as `[int, ..10]`.
Such a definite-sized vector type is a first-class type, since its size is known statically.
A vector without such a size is said to be of _indefinite_ size,
and is therefore not a _first-class_ type.
An indefinite-size vector can only be instantiated through a pointer type,
such as `&[T]` or `Vec<T>`.
The kind of a vector type depends on the kind of its element type,
as with other simple structural types.
Expressions producing vectors of definite size cannot be evaluated in a * `Vec<T>`, a 'vector'
context expecting a vector of indefinite size; one must copy the * `[T ..N]`, an 'array'
definite-sized vector contents into a distinct vector of indefinite size. * `&[T]`, a 'slice'.
An example of a vector type and its use: A vector is a heap-allocated list of `T`. A vector has ownership over the data
inside of it. It is also able to grow and change in size. It's important to note
that `Vec<T>` is a library type, it's not actually part of the core language.
~~~~ An array has a fixed size, and can be allocated on either the stack or the heap.
let v: &[int] = &[7, 5, 3];
let i: int = v[2];
assert!(i == 3);
~~~~
All in-bounds elements of a vector are always initialized, A slice is a 'view' into a vector or array. It doesn't own the data it points
and access to a vector is always bounds-checked. to, it borrows it.
An example of each kind:
```{rust}
let vec: Vec<int> = vec![1, 2, 3];
let arr: [int, ..3] = [1, 2, 3];
let s: &[int] = vec.as_slice();
```
As you can see, the `vec!` macro allows you to create a `Vec<T>` easily. The
`vec!` macro is also part of the standard library, rather than the language.
All in-bounds elements of vectors, arrays, and slices are always initialized,
and access to a vector, array, or slice is always bounds-checked.
### Structure types ### Structure types