Skip to content

Commit a021330

Browse files
committed
Fix vector/array/slice terminology in manual.
Fixes rust-lang#16015.
1 parent b516532 commit a021330

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

src/doc/rust.md

+25-23
Original file line numberDiff line numberDiff line change
@@ -3564,34 +3564,36 @@ let (a, b) = p;
35643564
assert!(b != "world");
35653565
~~~~
35663566

3567-
### Vector types
3567+
### Vector, Array, and Slice types
35683568

3569-
The vector type constructor represents a homogeneous array of values of a given type.
3570-
A vector has a fixed size.
3571-
(Operations like `vec.push` operate solely on owned vectors.)
3572-
A vector type can be annotated with a _definite_ size, such as `[int, ..10]`.
3573-
Such a definite-sized vector type is a first-class type, since its size is known statically.
3574-
A vector without such a size is said to be of _indefinite_ size,
3575-
and is therefore not a _first-class_ type.
3576-
An indefinite-size vector can only be instantiated through a pointer type,
3577-
such as `&[T]` or `Vec<T>`.
3578-
The kind of a vector type depends on the kind of its element type,
3579-
as with other simple structural types.
3569+
Rust has three different types for a list of items:
35803570

3581-
Expressions producing vectors of definite size cannot be evaluated in a
3582-
context expecting a vector of indefinite size; one must copy the
3583-
definite-sized vector contents into a distinct vector of indefinite size.
3571+
* `Vec<T>`, a 'vector'
3572+
* `[T ..N]`, an 'array'
3573+
* `&[T]`, a 'slice'.
35843574

3585-
An example of a vector type and its use:
3575+
A vector is a heap-allocated list of `T`. A vector has ownership over the data
3576+
inside of it. It is also able to grow and change in size. It's important to note
3577+
that `Vec<T>` is a library type, it's not actually part of the core language.
35863578

3587-
~~~~
3588-
let v: &[int] = &[7, 5, 3];
3589-
let i: int = v[2];
3590-
assert!(i == 3);
3591-
~~~~
3579+
An array has a fixed size, and can be allocated on either the stack or the heap.
3580+
3581+
A slice is a 'view' into a vector or array. It doesn't own the data it points
3582+
to, it borrows it.
3583+
3584+
An example of each kind:
3585+
3586+
```{rust}
3587+
let vec: Vec<int> = vec![1, 2, 3];
3588+
let arr: [int, ..3] = [1, 2, 3];
3589+
let s: &[int] = vec.as_slice();
3590+
```
3591+
3592+
As you can see, the `vec!` macro allows you to create a `Vec<T>` easily. The
3593+
`vec!` macro is also part of the standard library, rather than the language.
35923594

3593-
All in-bounds elements of a vector are always initialized,
3594-
and access to a vector is always bounds-checked.
3595+
All in-bounds elements of vectors, arrays, and slices are always initialized,
3596+
and access to a vector, array, or slice is always bounds-checked.
35953597

35963598
### Structure types
35973599

0 commit comments

Comments
 (0)