mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-30 06:43:20 +03:00
tutorial: add an example of freezing a managed box
This commit is contained in:
+15
-4
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user