@@ -3564,34 +3564,36 @@ let (a, b) = p;
3564
3564
assert!(b != "world");
3565
3565
~~~~
3566
3566
3567
- ### Vector types
3567
+ ### Vector, Array, and Slice types
3568
3568
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:
3580
3570
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' .
3584
3574
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.
3586
3578
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.
3592
3594
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.
3595
3597
3596
3598
### Structure types
3597
3599
0 commit comments