Skip to content

Commit 294337d

Browse files
committed
Add Vec visualization to understand capacity
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 rust-lang#76066
1 parent 8712efb commit 294337d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

alloc/src/vec/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,26 @@ mod spec_extend;
253253
/// can be slow. For this reason, it is recommended to use [`Vec::with_capacity`]
254254
/// whenever possible to specify how big the vector is expected to get.
255255
///
256+
/// A vector containing the elements `'a'` and `'b'` with capacity 4 can be
257+
/// visualized as:
258+
///
259+
/// ```text
260+
/// Stack ptr len capacity
261+
/// /Heap +--------+--------+--------+
262+
/// | 0x0123 | 2 | 4 |
263+
/// +--------+--------+--------+
264+
/// |
265+
/// v
266+
/// Heap +--------+--------+--------+--------+
267+
/// | 'a' | 'b' | uninit | uninit |
268+
/// +--------+--------+--------+--------+
269+
/// ```
270+
///
271+
/// - **uninit** represents memory that is not initialized, see [`MaybeUninit`].
272+
/// - Note: the ABI is not stable and `Vec` makes no guarantees about its memory
273+
/// layout (including the order of fields). See [the section about
274+
/// guarantees](#guarantees).
275+
///
256276
/// # Guarantees
257277
///
258278
/// Due to its incredibly fundamental nature, `Vec` makes a lot of guarantees
@@ -345,6 +365,7 @@ mod spec_extend;
345365
/// [`push`]: Vec::push
346366
/// [`insert`]: Vec::insert
347367
/// [`reserve`]: Vec::reserve
368+
/// [`MaybeUninit`]: core::mem::MaybeUninit
348369
/// [owned slice]: Box
349370
/// [slice]: ../../std/primitive.slice.html
350371
/// [`&`]: ../../std/primitive.reference.html

0 commit comments

Comments
 (0)