Skip to content

Commit a8406e3

Browse files
committed
tutorial: A little more explanation of why managed boxes are useful
1 parent 91fe3fc commit a8406e3

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

doc/tutorial.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,34 @@ let y = x; // Copy of a pointer to the same box
10521052
// then the allocation will be freed.
10531053
~~~~
10541054

1055+
Any type that contains managed boxes or other managed types is
1056+
considered _managed_. Managed types are the only types that can
1057+
construct cyclic data structures in Rust, such as doubly-linked lists.
1058+
1059+
~~~
1060+
// A linked list node
1061+
struct Node {
1062+
mut next: MaybeNode,
1063+
mut prev: MaybeNode,
1064+
payload: int
1065+
}
1066+
1067+
enum MaybeNode {
1068+
SomeNode(@Node),
1069+
NoNode
1070+
}
1071+
1072+
let node1 = @Node { next: NoNode, prev: NoNode, payload: 1 };
1073+
let node2 = @Node { next: NoNode, prev: NoNode, payload: 2 };
1074+
let node3 = @Node { next: NoNode, prev: NoNode, payload: 3 };
1075+
1076+
// Link the three list nodes together
1077+
node1.next = SomeNode(node2);
1078+
node2.prev = SomeNode(node1);
1079+
node2.next = SomeNode(node3);
1080+
node3.prev = SomeNode(node2);
1081+
~~~
1082+
10551083
Managed boxes never cross task boundaries.
10561084

10571085
> ***Note:*** managed boxes are currently reclaimed through reference
@@ -1060,7 +1088,7 @@ Managed boxes never cross task boundaries.
10601088
10611089
## Owned boxes
10621090

1063-
In contrast to maneged boxes, owned boxes have a single owning memory
1091+
In contrast to managed boxes, owned boxes have a single owning memory
10641092
slot and thus two owned boxes may not refer to the same memory. All
10651093
owned boxes across all tasks are allocated on a single _exchange
10661094
heap_, where their uniquely owned nature allows them to be passed

0 commit comments

Comments
 (0)