Skip to content

Commit 0189ef3

Browse files
committed
tutorial: add an example of freezing a managed box
1 parent f78af18 commit 0189ef3

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

doc/tutorial.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,10 +1173,7 @@ For a more in-depth explanation of borrowed pointers, read the
11731173
## Freezing
11741174
11751175
Borrowing an immutable pointer to an object freezes it and prevents mutation.
1176-
`Owned` objects have freezing enforced statically at compile-time. Mutable
1177-
managed boxes handle freezing dynamically when any of their contents are
1178-
borrowed, and the task will fail if an attempt to modify them is made while
1179-
they are frozen.
1176+
`Owned` objects have freezing enforced statically at compile-time.
11801177
11811178
~~~~
11821179
let mut x = 5;
@@ -1186,6 +1183,20 @@ let mut x = 5;
11861183
// x is now unfrozen again
11871184
~~~~
11881185
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+
11891200
# Dereferencing pointers
11901201
11911202
Rust uses the unary star operator (`*`) to access the contents of a

0 commit comments

Comments
 (0)