Skip to content

mentioned difference between memory allocation of arrays and vectors in book #20982

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

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 11 additions & 2 deletions src/doc/trpl/arrays-vectors-and-slices.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ languages.

A *vector* is a dynamic or "growable" array, implemented as the standard
library type [`Vec<T>`](../std/vec/) (we'll talk about what the `<T>` means
later). Vectors are to arrays what `String` is to `&str`. You can create them
with the `vec!` macro:
later). You can create them with the `vec!` macro:

```{rust}
let v = vec![1, 2, 3]; // v: Vec<i32>
Expand All @@ -60,6 +59,16 @@ let v = vec![1, 2, 3]; // v: Vec<i32>
brackets `[]` with `vec!`. Rust allows you to use either in either situation,
this is just convention.)

Vectors are to arrays what `String` is to `&str`. While the arrays are
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't correct. There's three core array-ish types:

  • [T; n]: Array
  • [T]: Slice
  • Vec<T>: Vector

[T; n] is "on the stack", because it has a compile-time-constant size.

[T] is a dynamically-sized type because it is a handle to some compile-time-unknown number of elements. In that sense, [T] can't ever exist on its own; it must exist behind a pointer as e.g. Box<[T]> or &[T]. Any such pointer becomes "fat" as it includes the number of elements in the slice as run-time information in addition to the actual address where the slice starts. A slice may just as easily refer to the contents of a Vec or Array.

Vec is a heap-allocated growable array, conceptually similar to a Box<[T]>, but without the requirement that capacity = length.

Vec == String, and &str == &[T], not [T; n]. There is no string analog to [T; n].

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, total brain fart. My brain read "slice". Sigh. You are totally correct.

allocated on the stack (unless we explicitly box them during allocation),
Vectors are always allocated on the heap.

```{rust}
let x = [0; 3]; // regular array, allocated on the stack
let y: Box<[isize; 3]> = Box::new([1, 2, 3]); // boxed array, heap allocated
let v = vec![1, 2, 3]; // vector, heap allocated
```

You can get the length of, iterate over, and subscript vectors just like
arrays. In addition, (mutable) vectors can grow automatically:

Expand Down