Skip to content

Commit f9bb7b7

Browse files
committed
Tutorial: make struct section more coherent
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 Signed-off-by: Luca Bruno <[email protected]>
1 parent 5ae76b5 commit f9bb7b7

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

doc/tutorial.md

+18-9
Original file line numberDiff line numberDiff line change
@@ -579,21 +579,30 @@ Structs are quite similar to C structs and are even laid out the same way in
579579
memory (so you can read from a Rust struct in C, and vice-versa). Use the dot
580580
operator to access struct fields, as in `mypoint.x`.
581581

582-
Inherited mutability means that any field of a struct may be mutable, if the
583-
struct is in a mutable slot (or a field of a struct in a mutable slot, and
584-
so forth).
585-
586582
~~~~
587-
struct Stack {
588-
content: ~[int],
589-
head: uint
583+
struct Point {
584+
x: float,
585+
y: float
590586
}
591587
~~~~
592588

593-
With a value (say, `mystack`) of such a type in a mutable location, you can do
594-
`mystack.head += 1`. But in an immutable location, such an assignment to a
589+
Inherited mutability means that any field of a struct may be mutable, if the
590+
struct is in a mutable slot (or a field of a struct in a mutable slot, and
591+
so forth).
592+
593+
With a value (say, `mypoint`) of such a type in a mutable location, you can do
594+
`mypoint.y += 1.0`. But in an immutable location, such an assignment to a
595595
struct without inherited mutability would result in a type error.
596596

597+
~~~~ {.xfail-test}
598+
# struct Point { x: float, y: float }
599+
let mut mypoint = Point { x: 1.0, y: 1.0 };
600+
let origin = Point { x: 0.0, y: 0.0 };
601+
602+
mypoint.y += 1.0; // mypoint is mutable, and its fields as well
603+
origin.y += 1.0; // ERROR: assigning to immutable field
604+
~~~~
605+
597606
`match` patterns destructure structs. The basic syntax is
598607
`Name { fieldname: pattern, ... }`:
599608

0 commit comments

Comments
 (0)