@@ -1002,11 +1002,46 @@ refer to that through a pointer.
10021002
10031003## Owned boxes
10041004
1005- An owned box (` ~ ` ) is a uniquely owned allocation on the heap. An owned box
1006- inherits the mutability and lifetime of the owner as it would if there was no
1007- box. The purpose of an owned box is to add a layer of indirection in order to
1008- create recursive data structures or cheaply pass around an object larger than a
1009- pointer.
1005+ An owned box (` ~ ` ) is a uniquely owned allocation on the heap. It inherits the
1006+ mutability and lifetime of the owner as it would if there was no box.
1007+
1008+ ~~~~
1009+ let x = 5; // immutable
1010+ let mut y = 5; // mutable
1011+ y += 2;
1012+
1013+ let x = ~5; // immutable
1014+ let mut y = ~5; // mutable
1015+ *y += 2; // the * operator is needed to access the contained value
1016+ ~~~~
1017+
1018+ The purpose of an owned box is to add a layer of indirection in order to create
1019+ recursive data structures or cheaply pass around an object larger than a
1020+ pointer. Since an owned box has a unique owner, it can be used to represent any
1021+ tree data structure.
1022+
1023+ The following struct won't compile, because the lack of indirection would mean
1024+ it has an infinite size:
1025+
1026+ ~~~~ {.xfail-test}
1027+ struct Foo {
1028+ child: Option<Foo>
1029+ }
1030+ ~~~~
1031+
1032+ > *** Note:*** The ` Option ` type is an enum that represents an * optional* value.
1033+ > It's comparable to a nullable pointer in many other languages, but stores the
1034+ > contained value unboxed.
1035+
1036+ Adding indirection with an owned pointer allocates the child outside of the
1037+ struct on the heap, which makes it a finite size and won't result in a
1038+ compile-time error:
1039+
1040+ ~~~~
1041+ struct Foo {
1042+ child: Option<~Foo>
1043+ }
1044+ ~~~~
10101045
10111046## Managed boxes
10121047
@@ -1018,6 +1053,20 @@ mutability. They do own the contained object, and mutability is defined by the
10181053type of the shared box (` @ ` or ` @mut ` ). An object containing a managed box is
10191054not ` Owned ` , and can't be sent between tasks.
10201055
1056+ ~~~~
1057+ let a = @5; // immutable
1058+
1059+ let mut b = @5; // mutable variable, immutable box
1060+ b = @10;
1061+
1062+ let c = @mut 5; // immutable variable, mutable box
1063+ *c = 10;
1064+
1065+ let mut d = @mut 5; // mutable variable, mutable box
1066+ *d += 5;
1067+ d = @mut 15;
1068+ ~~~~
1069+
10211070# Move semantics
10221071
10231072Rust uses a shallow copy for parameter passing, assignment and returning values
@@ -1035,10 +1084,10 @@ let z = x; // no new memory allocated, x can no longer be used
10351084# Borrowed pointers
10361085
10371086Rust's borrowed pointers are a general purpose reference type. In contrast with
1038- owned pointers , where the holder of an owned pointer is the owner of the
1039- pointed-to memory, borrowed pointers never imply ownership. A pointer can be
1040- borrowed to any object, and the compiler verifies that it cannot outlive the
1041- lifetime of the object.
1087+ owned boxes , where the holder of an owned box is the owner of the pointed-to
1088+ memory, borrowed pointers never imply ownership. A pointer can be borrowed to
1089+ any object, and the compiler verifies that it cannot outlive the lifetime of
1090+ the object.
10421091
10431092As an example, consider a simple struct type, ` Point ` :
10441093
@@ -1124,10 +1173,7 @@ For a more in-depth explanation of borrowed pointers, read the
11241173## Freezing
11251174
11261175Borrowing an immutable pointer to an object freezes it and prevents mutation.
1127- `Owned` objects have freezing enforced statically at compile-time. Mutable
1128- managed boxes handle freezing dynamically when any of their contents are
1129- borrowed, and the task will fail if an attempt to modify them is made while
1130- they are frozen.
1176+ `Owned` objects have freezing enforced statically at compile-time.
11311177
11321178~~~~
11331179let mut x = 5;
@@ -1137,6 +1183,20 @@ let mut x = 5;
11371183// x is now unfrozen again
11381184~~~~
11391185
1186+ Mutable managed boxes handle freezing dynamically when any of their contents
1187+ are borrowed, and the task will fail if an attempt to modify them is made while
1188+ they are frozen:
1189+
1190+ ~~~~
1191+ let x = @mut 5;
1192+ let y = x;
1193+ {
1194+ let y = &*y; // the managed box is now frozen
1195+ // modifying it through x or y will cause a task failure
1196+ }
1197+ // the box is now unfrozen again
1198+ ~~~~
1199+
11401200# Dereferencing pointers
11411201
11421202Rust uses the unary star operator (`*`) to access the contents of a
0 commit comments