Skip to content

Commit

Permalink
Add Vec visualization to understand capacity
Browse files Browse the repository at this point in the history
Visualize vector while differentiating between stack and heap.

Inspired by cheats.rs, as this is probably the first place beginner go,
they could understand stack and heap, length and capacity with this. Not
sure if adding this means we should add to other places too.

Superseeds #76066
  • Loading branch information
pickfire committed Jan 20, 2021
1 parent a4cbb44 commit 9e42d14
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions library/alloc/src/vec/mod.rs
Expand Up @@ -253,6 +253,26 @@ mod spec_extend;
/// can be slow. For this reason, it is recommended to use [`Vec::with_capacity`]
/// whenever possible to specify how big the vector is expected to get.
///
/// A vector containing the elements `'a'` and `'b'` with capacity 4 can be
/// visualized as:
///
/// ```text
/// Stack ptr len capacity
/// /Heap +--------+--------+--------+
/// | 0x0123 | 2 | 4 |
/// +--------+--------+--------+
/// |
/// v
/// Heap +--------+--------+--------+--------+
/// | 'a' | 'b' | uninit | uninit |
/// +--------+--------+--------+--------+
/// ```
///
/// - **uninit** represents memory that is not initialized, see [`MaybeUninit`].
/// - Note: the ABI is not stable and `Vec` makes no guarantees about its memory
/// layout (including the order of fields). See [the section about
/// guarantees](#guarantees).
///
/// # Guarantees
///
/// Due to its incredibly fundamental nature, `Vec` makes a lot of guarantees
Expand Down Expand Up @@ -345,6 +365,7 @@ mod spec_extend;
/// [`push`]: Vec::push
/// [`insert`]: Vec::insert
/// [`reserve`]: Vec::reserve
/// [`MaybeUninit`]: core::mem::MaybeUninit
/// [owned slice]: Box
/// [slice]: ../../std/primitive.slice.html
/// [`&`]: ../../std/primitive.reference.html
Expand Down

0 comments on commit 9e42d14

Please sign in to comment.