Skip to content

Commit ee597a8

Browse files
committed
Auto merge of #30292 - Xmasreturns:patch-3, r=steveklabnik
Updated structs.md in the book
2 parents f97e4d3 + 2adba31 commit ee597a8

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

src/doc/book/structs.md

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ let origin_x = 0;
99
let origin_y = 0;
1010
```
1111

12-
A `struct` lets us combine these two into a single, unified datatype:
12+
A `struct` lets us combine these two into a single, unified datatype with `x`
13+
and `y` as field labels:
1314

1415
```rust
1516
struct Point {
@@ -32,7 +33,7 @@ We can create an instance of our `struct` via `let`, as usual, but we use a `key
3233
value` style syntax to set each field. The order doesn’t need to be the same as
3334
in the original declaration.
3435

35-
Finally, because fields have names, we can access the field through dot
36+
Finally, because fields have names, we can access them through dot
3637
notation: `origin.x`.
3738

3839
The values in `struct`s are immutable by default, like other bindings in Rust.
@@ -67,9 +68,8 @@ struct Point {
6768

6869
Mutability is a property of the binding, not of the structure itself. If you’re
6970
used to field-level mutability, this may seem strange at first, but it
70-
significantly simplifies things. It even lets you make things mutable for a short
71-
time only:
72-
71+
significantly simplifies things. It even lets you make things mutable on a temporary
72+
basis:
7373

7474
```rust,ignore
7575
struct Point {
@@ -82,7 +82,7 @@ fn main() {
8282
8383
point.x = 5;
8484
85-
let point = point; // this new binding can’t change now
85+
let point = point; // now immutable
8686
8787
point.y = 6; // this causes an error
8888
}
@@ -121,27 +121,24 @@ let point = Point3d { z: 1, x: 2, .. origin };
121121
# Tuple structs
122122

123123
Rust has another data type that’s like a hybrid between a [tuple][tuple] and a
124-
`struct`, called a ‘tuple struct’. Tuple structs have a name, but
125-
their fields don’t:
124+
`struct`, called a ‘tuple struct’. Tuple structs have a name, but their fields
125+
don't. They are declared with the `struct` keyword, and then with a name
126+
followed by a tuple:
127+
128+
[tuple]: primitive-types.html#tuples
126129

127130
```rust
128131
struct Color(i32, i32, i32);
129132
struct Point(i32, i32, i32);
130-
```
131133

132-
[tuple]: primitive-types.html#tuples
133-
134-
These two will not be equal, even if they have the same values:
135-
136-
```rust
137-
# struct Color(i32, i32, i32);
138-
# struct Point(i32, i32, i32);
139134
let black = Color(0, 0, 0);
140135
let origin = Point(0, 0, 0);
141136
```
137+
Here, `black` and `origin` are not equal, even though they contain the same
138+
values.
142139

143-
It is almost always better to use a `struct` than a tuple struct. We would write
144-
`Color` and `Point` like this instead:
140+
It is almost always better to use a `struct` than a tuple struct. We
141+
would write `Color` and `Point` like this instead:
145142

146143
```rust
147144
struct Color {
@@ -157,13 +154,14 @@ struct Point {
157154
}
158155
```
159156

160-
Now, we have actual names, rather than positions. Good names are important,
161-
and with a `struct`, we have actual names.
157+
Good names are important, and while values in a tuple struct can be
158+
referenced with dot notation as well, a `struct` gives us actual names,
159+
rather than positions.
162160

163-
There _is_ one case when a tuple struct is very useful, though, and that’s a
164-
tuple struct with only one element. We call this the ‘newtype’ pattern, because
165-
it allows you to create a new type, distinct from that of its contained value
166-
and expressing its own semantic meaning:
161+
There _is_ one case when a tuple struct is very useful, though, and that is when
162+
it has only one element. We call this the ‘newtype’ pattern, because
163+
it allows you to create a new type that is distinct from its contained value
164+
and also expresses its own semantic meaning:
167165

168166
```rust
169167
struct Inches(i32);

0 commit comments

Comments
 (0)