Skip to content

Commit

Permalink
auto merge of #5441 : lucab/rust/lucab/tutorial/struct, r=z0w0
Browse files Browse the repository at this point in the history
In struct section of tutorial, make everything more coherent and
clear by always using "struct Point".
Also, do not prematurely introduce pointers and arrays.
Fixes #5240
  • Loading branch information
bors committed Mar 22, 2013
2 parents 80d47fd + f9bb7b7 commit d8c0da3
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -579,21 +579,30 @@ Structs are quite similar to C structs and are even laid out the same way in
memory (so you can read from a Rust struct in C, and vice-versa). Use the dot
operator to access struct fields, as in `mypoint.x`.
Inherited mutability means that any field of a struct may be mutable, if the
struct is in a mutable slot (or a field of a struct in a mutable slot, and
so forth).
~~~~
struct Stack {
content: ~[int],
head: uint
struct Point {
x: float,
y: float
}
~~~~
With a value (say, `mystack`) of such a type in a mutable location, you can do
`mystack.head += 1`. But in an immutable location, such an assignment to a
Inherited mutability means that any field of a struct may be mutable, if the
struct is in a mutable slot (or a field of a struct in a mutable slot, and
so forth).
With a value (say, `mypoint`) of such a type in a mutable location, you can do
`mypoint.y += 1.0`. But in an immutable location, such an assignment to a
struct without inherited mutability would result in a type error.
~~~~ {.xfail-test}
# struct Point { x: float, y: float }
let mut mypoint = Point { x: 1.0, y: 1.0 };
let origin = Point { x: 0.0, y: 0.0 };
mypoint.y += 1.0; // mypoint is mutable, and its fields as well
origin.y += 1.0; // ERROR: assigning to immutable field
~~~~

`match` patterns destructure structs. The basic syntax is
`Name { fieldname: pattern, ... }`:

Expand Down

0 comments on commit d8c0da3

Please sign in to comment.