11% Structs
22
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
44doing calculations involving coordinates in 2D space, we would need both an ` x `
55and a ` y ` value:
66
@@ -9,7 +9,7 @@ let origin_x = 0;
99let 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:
1313
1414``` rust
1515struct Point {
@@ -28,14 +28,14 @@ There’s a lot going on here, so let’s break it down. We declare a `struct` w
2828the ` struct ` keyword, and then with a name. By convention, ` struct ` s begin with
2929a capital letter and are camel cased: ` PointInSpace ` , not ` Point_In_Space ` .
3030
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:
3232value` style syntax to set each field. The order doesn’t need to be the same as
3333in the original declaration.
3434
3535Finally, because fields have names, we can access the field through dot
3636notation: ` origin.x ` .
3737
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.
3939Use ` mut ` to make them mutable:
4040
4141``` rust
@@ -91,7 +91,7 @@ fn main() {
9191# Update syntax
9292
9393A ` 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:
9595
9696``` rust
9797struct Point3d {
@@ -121,7 +121,7 @@ let point = Point3d { z: 1, x: 2, .. origin };
121121# Tuple structs
122122
123123Rust 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
125125their fields don’t:
126126
127127``` rust
@@ -140,7 +140,7 @@ let black = Color(0, 0, 0);
140140let origin = Point (0 , 0 , 0 );
141141```
142142
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
144144` Color ` and ` Point ` like this instead:
145145
146146``` rust
@@ -158,7 +158,7 @@ struct Point {
158158```
159159
160160Now, 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.
162162
163163There _ is_ one case when a tuple struct is very useful, though, and that’s a
164164tuple 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
180180
181181# Unit-like structs
182182
183- You can define a struct with no members at all:
183+ You can define a ` struct ` with no members at all:
184184
185185``` rust
186186struct Electron ;
187187```
188188
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
190190tuple, ` () ` , sometimes called ‘unit’. Like a tuple struct, it defines a
191191new type.
192192
@@ -195,6 +195,6 @@ marker type), but in combination with other features, it can become
195195useful. For instance, a library may ask you to create a structure that
196196implements a certain [ trait] [ trait ] to handle events. If you don’t have
197197any data you need to store in the structure, you can just create a
198- unit-like struct.
198+ unit-like ` struct ` .
199199
200200[ trait ] : traits.html
0 commit comments