Skip to content

Commit f9fe658

Browse files
committed
Clarify contiguous memory array structure of vectors in documentation
Closes #31554. Contributes to #29380.
1 parent a888333 commit f9fe658

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/doc/book/vectors.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,21 @@ let v = vec![1, 2, 3, 4, 5]; // v: Vec<i32>
1111
```
1212

1313
(Notice that unlike the `println!` macro we’ve used in the past, we use square
14-
brackets `[]` with `vec!` macro. Rust allows you to use either in either situation,
15-
this is just convention.)
14+
brackets `[]` with `vec!` macro. Rust allows you to use either in either
15+
situation, this is just convention.)
1616

1717
There’s an alternate form of `vec!` for repeating an initial value:
1818

1919
```rust
2020
let v = vec![0; 10]; // ten zeroes
2121
```
2222

23+
Vectors store their contents as contiguous arrays of `T` on the heap. This means
24+
that they must be able to know the size of `T` at compile time (that is, how
25+
many bytes are needed to store a `T`?). The size of some things can't be known
26+
at compile time. For these you'll have to store a pointer to that thing:
27+
thankfully, the [`Box`][box] type works perfectly for this.
28+
2329
## Accessing elements
2430

2531
To get the value at a particular index in the vector, we use `[]`s:
@@ -113,6 +119,7 @@ Vectors have many more useful methods, which you can read about in [their
113119
API documentation][vec].
114120

115121
[vec]: ../std/vec/index.html
122+
[box]: ../std/boxed/index.html
116123
[generic]: generics.html
117124
[panic]: concurrency.html#panics
118125
[get]: http://doc.rust-lang.org/std/vec/struct.Vec.html#method.get

src/libcollections/vec.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! A growable list type with heap-allocated contents, written `Vec<T>` but
12-
//! pronounced 'vector.'
11+
//! A contiguous growable array type with heap-allocated contents, written
12+
//! `Vec<T>` but pronounced 'vector.'
1313
//!
1414
//! Vectors have `O(1)` indexing, amortized `O(1)` push (to the end) and
1515
//! `O(1)` pop (from the end).
@@ -78,7 +78,7 @@ use borrow::{Cow, IntoCow};
7878

7979
use super::range::RangeArgument;
8080

81-
/// A growable list type, written `Vec<T>` but pronounced 'vector.'
81+
/// A contiguous growable array type, written `Vec<T>` but pronounced 'vector.'
8282
///
8383
/// # Examples
8484
///

0 commit comments

Comments
 (0)