diff options
| author | bors <bors@rust-lang.org> | 2013-04-05 18:54:52 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-04-05 18:54:52 -0700 |
| commit | 614d6da828969389871e01d1b8a17b3ccc1a5caa (patch) | |
| tree | fbab1f12447a5bd53882c7364974193a8035c59b | |
| parent | 08e2cf846aebf5a9f5e53881814976a3beee89a7 (diff) | |
| parent | e2a6feb8fe38be01512f0fd1ea08b1909f190892 (diff) | |
| download | rust-614d6da828969389871e01d1b8a17b3ccc1a5caa.tar.gz rust-614d6da828969389871e01d1b8a17b3ccc1a5caa.zip | |
auto merge of #5721 : dhardy/rust/master, r=graydon
This is some stuff which seemed to be missing to me (though I haven't read everything yet so hope I haven't missed the relevant section). A similar addition for borrowing handles is needed, but #5720 stumped me. Comments welcome.
| -rw-r--r-- | doc/tutorial.md | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/doc/tutorial.md b/doc/tutorial.md index aeda158d0d3..902a7902972 100644 --- a/doc/tutorial.md +++ b/doc/tutorial.md @@ -1067,6 +1067,28 @@ let mut d = @mut 5; // mutable variable, mutable box d = @mut 15; ~~~~ +A mutable variable and an immutable variable can refer to the same box, given +that their types are compatible. Mutability of a box is a property of its type, +however, so for example a mutable handle to an immutable box cannot be +assigned a reference to a mutable box. + +~~~~ +let a = @1; // immutable box +let b = @mut 2; // mutable box + +let mut c : @int; // declare a variable with type managed immutable int +let mut d : @mut int; // and one of type managed mutable int + +c = a; // box type is the same, okay +d = b; // box type is the same, okay +~~~~ + +~~~~ {.xfail-test} +// but b cannot be assigned to c, or a to d +c = b; // error +~~~~ + + # Move semantics Rust uses a shallow copy for parameter passing, assignment and returning values @@ -1081,6 +1103,16 @@ let y = x.clone(); // y is a newly allocated box let z = x; // no new memory allocated, x can no longer be used ~~~~ +Since in owned boxes mutability is a property of the owner, not the +box, mutable boxes may become immutable when they are moved, and vice-versa. + +~~~~ +let r = ~13; +let mut s = r; // box becomes mutable +*s += 1; +let t = s; // box becomes immutable +~~~~ + # Borrowed pointers Rust's borrowed pointers are a general purpose reference type. In contrast with |
