From cb91e914185f4be9073dcec9a96ca6b78b7e877f Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Thu, 4 Apr 2013 15:08:25 +0200 Subject: [PATCH 1/4] Tutorial: rename variable to avoid confusion. --- doc/tutorial.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tutorial.md b/doc/tutorial.md index 42b0d5a585aee..c7d92c03b6d1d 100644 --- a/doc/tutorial.md +++ b/doc/tutorial.md @@ -1191,7 +1191,7 @@ they are frozen: let x = @mut 5; let y = x; { - let y = &*y; // the managed box is now frozen + let z = &*y; // the managed box is now frozen // modifying it through x or y will cause a task failure } // the box is now unfrozen again From 964fc862e061a01a399c537bc36b076e672ffaa1 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Thu, 4 Apr 2013 18:13:12 +0200 Subject: [PATCH 2/4] Tutorial: comment on how mutability applies to boxes --- doc/tutorial.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/doc/tutorial.md b/doc/tutorial.md index c7d92c03b6d1d..54340c0a4c10d 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 From 1e483c7b70a7369da3c1cd2c858cf2c3d455968c Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Thu, 4 Apr 2013 22:35:23 +0200 Subject: [PATCH 3/4] Tutorial: fix typo --- doc/tutorial.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/tutorial.md b/doc/tutorial.md index 54340c0a4c10d..794f3089a100d 100644 --- a/doc/tutorial.md +++ b/doc/tutorial.md @@ -1069,8 +1069,8 @@ 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. +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 @@ -1079,8 +1079,8 @@ 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 +c = a; // box type is the same, okay +d = b; // box type is the same, okay // but b cannot be assigned to c, or a to d c = b; // error From e2a6feb8fe38be01512f0fd1ea08b1909f190892 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Fri, 5 Apr 2013 12:26:47 +0200 Subject: [PATCH 4/4] Tutorial: spelling correction and move a failing test to an xfail-test marked code block. --- doc/tutorial.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/tutorial.md b/doc/tutorial.md index 794f3089a100d..5a47cefb33d2c 100644 --- a/doc/tutorial.md +++ b/doc/tutorial.md @@ -1081,7 +1081,9 @@ 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 ~~~~ @@ -1101,7 +1103,7 @@ 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 +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. ~~~~