tutorial: add an example of freezing a managed box

This commit is contained in:
Daniel Micay 2013-03-29 15:34:14 -04:00
parent f78af18127
commit 0189ef3600

View File

@ -1173,10 +1173,7 @@ For a more in-depth explanation of borrowed pointers, read the
## Freezing
Borrowing an immutable pointer to an object freezes it and prevents mutation.
`Owned` objects have freezing enforced statically at compile-time. Mutable
managed boxes handle freezing dynamically when any of their contents are
borrowed, and the task will fail if an attempt to modify them is made while
they are frozen.
`Owned` objects have freezing enforced statically at compile-time.
~~~~
let mut x = 5;
@ -1186,6 +1183,20 @@ let mut x = 5;
// x is now unfrozen again
~~~~
Mutable managed boxes handle freezing dynamically when any of their contents
are borrowed, and the task will fail if an attempt to modify them is made while
they are frozen:
~~~~
let x = @mut 5;
let y = x;
{
let y = &*y; // the managed box is now frozen
// modifying it through x or y will cause a task failure
}
// the box is now unfrozen again
~~~~
# Dereferencing pointers
Rust uses the unary star operator (`*`) to access the contents of a