Skip to content

Commit 1a9641b

Browse files
committed
auto merge of #11567 : divtxt/rust/master, r=cmr
I found the boxes diagram in the tutorial misleading about how the enum worked. The current diagram makes it seem that there is a separate Cons struct when there is only one type of struct for the List type, and Nil is drawn almost as if it's not consuming memory. I'm aware of the optimization that happens for this enum which takes advantage of the fact that pointer cannot be null but this is an implementation detail and not the only one that applies here. I can add a note below the diagram mentioning this if you like.
2 parents dbce62c + 8f93d39 commit 1a9641b

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

doc/tutorial.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,10 +1020,15 @@ being destroyed along with the owner. Since the `list` variable above is
10201020
immutable, the whole list is immutable. The memory allocation itself is the
10211021
box, while the owner holds onto a pointer to it:
10221022

1023-
Cons cell Cons cell Cons cell
1024-
+-----------+ +-----+-----+ +-----+-----+
1025-
| 1 | ~ | -> | 2 | ~ | -> | 3 | ~ | -> Nil
1026-
+-----------+ +-----+-----+ +-----+-----+
1023+
List box List box List box List box
1024+
+--------------+ +--------------+ +--------------+ +--------------+
1025+
list -> | Cons | 1 | ~ | -> | Cons | 2 | ~ | -> | Cons | 3 | ~ | -> | Nil |
1026+
+--------------+ +--------------+ +--------------+ +--------------+
1027+
1028+
> Note: the above diagram shows the logical contents of the enum. The actual
1029+
> memory layout of the enum may vary. For example, for the `List` enum shown
1030+
> above, Rust guarantees that there will be no enum tag field in the actual
1031+
> structure. See the language reference for more details.
10271032
10281033
An owned box is a common example of a type with a destructor. The allocated
10291034
memory is cleaned up when the box is destroyed.

0 commit comments

Comments
 (0)