about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDiggory Hardy <diggory.hardy@gmail.com>2013-04-04 18:13:12 +0200
committerDiggory Hardy <diggory.hardy@gmail.com>2013-04-04 18:13:12 +0200
commit964fc862e061a01a399c537bc36b076e672ffaa1 (patch)
treee8f3793be560c2f745968820755d46147d07d5f0
parentcb91e914185f4be9073dcec9a96ca6b78b7e877f (diff)
downloadrust-964fc862e061a01a399c537bc36b076e672ffaa1.tar.gz
rust-964fc862e061a01a399c537bc36b076e672ffaa1.zip
Tutorial: comment on how mutability applies to boxes
-rw-r--r--doc/tutorial.md30
1 files changed, 30 insertions, 0 deletions
diff --git a/doc/tutorial.md b/doc/tutorial.md
index c7d92c03b6d..54340c0a4c1 100644
--- a/doc/tutorial.md
+++ b/doc/tutorial.md
@@ -1067,6 +1067,26 @@ 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 hande 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
+d = b;          // box type is the same
+
+// 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 +1101,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 mutabilility 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