Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection of fixes for the 'Vectors` chapter. #1216

Merged
merged 3 commits into from
Jul 15, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/std/vec.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

Vectors are re-sizable arrays. Like slices, their size is not known at compile
time, but they can grow or shrink at any time. A vector is represented using
3 words: a pointer to the data, its length, and its capacity. The capacity
indicates how much memory is reserved for the vector. The vector can grow as
long as the length is smaller than the capacity. When this threshold needs to
be surpassed, the vector is reallocated with a larger capacity.
3 parameters:
- pointer to the data
- length
- capacity

The capacity indicates how much memory is reserved for the vector. The vector
can grow as long as the length is smaller than the capacity. When this threshold
needs to be surpassed, the vector is reallocated with a larger capacity.

```rust,editable,ignore,mdbook-runnable
fn main() {
// Iterators can be collected into vectors
let mut collected_iterator: Vec<i32> = (0..10).collect();
let collected_iterator: Vec<i32> = (0..10).collect();
println!("Collected (0..10) into: {:?}", collected_iterator);

// The `vec!` macro can be used to initialize a vector
Expand All @@ -26,8 +30,8 @@ fn main() {
collected_iterator.push(0);
// FIXME ^ Comment out this line

// The `len` method yields the current size of the vector
println!("Vector size: {}", xs.len());
// The `len` method yields the number of elements currently stored in a vector
println!("Vector length: {}", xs.len());

// Indexing is done using the square brackets (indexing starts at 0)
println!("Second element: {}", xs[1]);
Expand Down