Clarify contiguous memory array structure of vectors in documentation
Closes #31554. Contributes to #29380.
This commit is contained in:
parent
17691af38d
commit
f2bea1cb70
2 changed files with 12 additions and 5 deletions
|
@ -11,8 +11,8 @@ let v = vec![1, 2, 3, 4, 5]; // v: Vec<i32>
|
||||||
```
|
```
|
||||||
|
|
||||||
(Notice that unlike the `println!` macro we’ve used in the past, we use square
|
(Notice that unlike the `println!` macro we’ve used in the past, we use square
|
||||||
brackets `[]` with `vec!` macro. Rust allows you to use either in either situation,
|
brackets `[]` with `vec!` macro. Rust allows you to use either in either
|
||||||
this is just convention.)
|
situation, this is just convention.)
|
||||||
|
|
||||||
There’s an alternate form of `vec!` for repeating an initial value:
|
There’s an alternate form of `vec!` for repeating an initial value:
|
||||||
|
|
||||||
|
@ -20,6 +20,12 @@ There’s an alternate form of `vec!` for repeating an initial value:
|
||||||
let v = vec![0; 10]; // ten zeroes
|
let v = vec![0; 10]; // ten zeroes
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Vectors store their contents as contiguous arrays of `T` on the heap. This means
|
||||||
|
that they must be able to know the size of `T` at compile time (that is, how
|
||||||
|
many bytes are needed to store a `T`?). The size of some things can't be known
|
||||||
|
at compile time. For these you'll have to store a pointer to that thing:
|
||||||
|
thankfully, the [`Box`][box] type works perfectly for this.
|
||||||
|
|
||||||
## Accessing elements
|
## Accessing elements
|
||||||
|
|
||||||
To get the value at a particular index in the vector, we use `[]`s:
|
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
|
||||||
API documentation][vec].
|
API documentation][vec].
|
||||||
|
|
||||||
[vec]: ../std/vec/index.html
|
[vec]: ../std/vec/index.html
|
||||||
|
[box]: ../std/boxed/index.html
|
||||||
[generic]: generics.html
|
[generic]: generics.html
|
||||||
[panic]: concurrency.html#panics
|
[panic]: concurrency.html#panics
|
||||||
[get]: http://doc.rust-lang.org/std/vec/struct.Vec.html#method.get
|
[get]: http://doc.rust-lang.org/std/vec/struct.Vec.html#method.get
|
||||||
|
|
|
@ -8,8 +8,8 @@
|
||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
//! A growable list type with heap-allocated contents, written `Vec<T>` but
|
//! A contiguous growable array type with heap-allocated contents, written
|
||||||
//! pronounced 'vector.'
|
//! `Vec<T>` but pronounced 'vector.'
|
||||||
//!
|
//!
|
||||||
//! Vectors have `O(1)` indexing, amortized `O(1)` push (to the end) and
|
//! Vectors have `O(1)` indexing, amortized `O(1)` push (to the end) and
|
||||||
//! `O(1)` pop (from the end).
|
//! `O(1)` pop (from the end).
|
||||||
|
@ -78,7 +78,7 @@ use borrow::{Cow, IntoCow};
|
||||||
|
|
||||||
use super::range::RangeArgument;
|
use super::range::RangeArgument;
|
||||||
|
|
||||||
/// A growable list type, written `Vec<T>` but pronounced 'vector.'
|
/// A contiguous growable array type, written `Vec<T>` but pronounced 'vector.'
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue