1
1
% Structs
2
2
3
- Structs are a way of creating more complex data types. For example, if we were
3
+ ` struct ` s are a way of creating more complex data types. For example, if we were
4
4
doing calculations involving coordinates in 2D space, we would need both an ` x `
5
5
and a ` y ` value:
6
6
@@ -9,7 +9,7 @@ let origin_x = 0;
9
9
let origin_y = 0 ;
10
10
```
11
11
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:
13
13
14
14
``` rust
15
15
struct Point {
@@ -28,14 +28,14 @@ There’s a lot going on here, so let’s break it down. We declare a `struct` w
28
28
the ` struct ` keyword, and then with a name. By convention, ` struct ` s begin with
29
29
a capital letter and are camel cased: ` PointInSpace ` , not ` Point_In_Space ` .
30
30
31
- We can create an instance of our struct via ` let ` , as usual, but we use a `key:
31
+ We can create an instance of our ` struct ` via ` let ` , as usual, but we use a `key:
32
32
value` style syntax to set each field. The order doesn’t need to be the same as
33
33
in the original declaration.
34
34
35
35
Finally, because fields have names, we can access the field through dot
36
36
notation: ` origin.x ` .
37
37
38
- The values in structs are immutable by default, like other bindings in Rust.
38
+ The values in ` struct ` s are immutable by default, like other bindings in Rust.
39
39
Use ` mut ` to make them mutable:
40
40
41
41
``` rust
@@ -91,7 +91,7 @@ fn main() {
91
91
# Update syntax
92
92
93
93
A ` struct ` can include ` .. ` to indicate that you want to use a copy of some
94
- other struct for some of the values. For example:
94
+ other ` struct ` for some of the values. For example:
95
95
96
96
``` rust
97
97
struct Point3d {
@@ -121,7 +121,7 @@ let point = Point3d { z: 1, x: 2, .. origin };
121
121
# Tuple structs
122
122
123
123
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
124
+ ` struct ` , called a ‘tuple struct’. Tuple structs have a name, but
125
125
their fields don’t:
126
126
127
127
``` rust
@@ -140,7 +140,7 @@ let black = Color(0, 0, 0);
140
140
let origin = Point (0 , 0 , 0 );
141
141
```
142
142
143
- It is almost always better to use a struct than a tuple struct. We would write
143
+ It is almost always better to use a ` struct ` than a tuple struct. We would write
144
144
` Color ` and ` Point ` like this instead:
145
145
146
146
``` rust
@@ -158,7 +158,7 @@ struct Point {
158
158
```
159
159
160
160
Now, we have actual names, rather than positions. Good names are important,
161
- and with a struct, we have actual names.
161
+ and with a ` struct ` , we have actual names.
162
162
163
163
There _ is_ one case when a tuple struct is very useful, though, and that’s a
164
164
tuple struct with only one element. We call this the ‘newtype’ pattern, because
@@ -180,13 +180,13 @@ destructuring `let`, just as with regular tuples. In this case, the
180
180
181
181
# Unit-like structs
182
182
183
- You can define a struct with no members at all:
183
+ You can define a ` struct ` with no members at all:
184
184
185
185
``` rust
186
186
struct Electron ;
187
187
```
188
188
189
- Such a struct is called ‘unit-like’ because it resembles the empty
189
+ Such a ` struct ` is called ‘unit-like’ because it resembles the empty
190
190
tuple, ` () ` , sometimes called ‘unit’. Like a tuple struct, it defines a
191
191
new type.
192
192
@@ -195,6 +195,6 @@ marker type), but in combination with other features, it can become
195
195
useful. For instance, a library may ask you to create a structure that
196
196
implements a certain [ trait] [ trait ] to handle events. If you don’t have
197
197
any data you need to store in the structure, you can just create a
198
- unit-like struct.
198
+ unit-like ` struct ` .
199
199
200
200
[ trait ] : traits.html
0 commit comments