@@ -1002,11 +1002,46 @@ refer to that through a pointer.
1002
1002
1003
1003
## Owned boxes
1004
1004
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
+ ~~~~
1010
1045
1011
1046
## Managed boxes
1012
1047
@@ -1018,6 +1053,20 @@ mutability. They do own the contained object, and mutability is defined by the
1018
1053
type of the shared box (` @ ` or ` @mut ` ). An object containing a managed box is
1019
1054
not ` Owned ` , and can't be sent between tasks.
1020
1055
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
+
1021
1070
# Move semantics
1022
1071
1023
1072
Rust 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
1035
1084
# Borrowed pointers
1036
1085
1037
1086
Rust'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.
1042
1091
1043
1092
As an example, consider a simple struct type, ` Point ` :
1044
1093
@@ -1124,10 +1173,7 @@ For a more in-depth explanation of borrowed pointers, read the
1124
1173
## Freezing
1125
1174
1126
1175
Borrowing 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.
1131
1177
1132
1178
~~~~
1133
1179
let mut x = 5;
@@ -1137,6 +1183,20 @@ let mut x = 5;
1137
1183
// x is now unfrozen again
1138
1184
~~~~
1139
1185
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
+
1140
1200
# Dereferencing pointers
1141
1201
1142
1202
Rust uses the unary star operator (`*`) to access the contents of a
0 commit comments