@@ -59,21 +59,109 @@ mod let_keyword { }
59
59
60
60
#[ doc( keyword = "struct" ) ]
61
61
//
62
- /// The `struct` keyword.
62
+ /// The keyword used to define structs .
63
63
///
64
- /// The `struct` keyword is used to define a struct type.
64
+ /// Structs in Rust come in three flavours: Regular structs, tuple structs,
65
+ /// and empty structs.
65
66
///
66
- /// Example:
67
+ /// ```rust
68
+ /// struct Regular {
69
+ /// field1: f32,
70
+ /// field2: String,
71
+ /// pub field3: bool
72
+ /// }
67
73
///
74
+ /// struct Tuple(u32, String);
75
+ ///
76
+ /// struct Empty;
68
77
/// ```
69
- /// struct Foo {
70
- /// field1: u32,
71
- /// field2: String,
78
+ ///
79
+ /// Regular structs are the most commonly used. Each field defined within them has a name and a
80
+ /// type, and once defined can be accessed using `example_struct.field` syntax. The fields of a
81
+ /// struct share its mutability, so `foo.bar = 2;` would only be valid if `foo` was mutable. Adding
82
+ /// `pub` to a field makes it visible to code in other modules, as well as allowing it to be
83
+ /// directly accessed and modified.
84
+ ///
85
+ /// Tuple structs are similar to regular structs, but its fields have no names. They are used like
86
+ /// tuples, with deconstruction possible via `let TupleStruct(x, y) = foo;` syntax. For accessing
87
+ /// individual variables, the same syntax is used as with regular tuples, namely `foo.0`, `foo.1`,
88
+ /// etc, starting at zero.
89
+ ///
90
+ /// Empty structs, or unit-like structs, are most commonly used as markers, for example
91
+ /// [`PhantomData`]. Empty structs have a size of zero bytes, but unlike empty enums they can be
92
+ /// instantiated, making them similar to the unit type `()`. Unit-like structs are useful when you
93
+ /// need to implement a trait on something, but don't need to store any data inside it.
94
+ ///
95
+ /// # Instantiation
96
+ ///
97
+ /// Structs can be instantiated in a manner of different ways, each of which can be mixed and
98
+ /// matched as needed. The most common way to make a new struct is via a constructor method such as
99
+ /// `new()`, but when that isn't available (or you're writing the constructor itself), struct
100
+ /// literal syntax is used:
101
+ ///
102
+ /// ```rust
103
+ /// # struct Foo { field1: f32, field2: String, etc: bool }
104
+ /// let example = Foo {
105
+ /// field1: 42.0,
106
+ /// field2: "blah".to_string(),
107
+ /// etc: true,
108
+ /// };
109
+ /// ```
110
+ ///
111
+ /// It's only possible to directly instantiate a struct using struct literal syntax when all of its
112
+ /// fields are visible to you.
113
+ ///
114
+ /// There are a handful of shortcuts provided to make writing constructors more convenient, most
115
+ /// common of which is the Field Init shorthand. When there is a variable and a field of the same
116
+ /// name, the assignment can be simplified from `field: field` into simply `field`. The following
117
+ /// example of a hypothetical constructor demonstrates this:
118
+ ///
119
+ /// ```rust
120
+ /// struct User {
121
+ /// name: String,
122
+ /// admin: bool,
123
+ /// }
124
+ ///
125
+ /// impl User {
126
+ /// pub fn new(name: String) -> Self {
127
+ /// Self {
128
+ /// name,
129
+ /// admin: false,
130
+ /// }
131
+ /// }
72
132
/// }
73
133
/// ```
74
134
///
75
- /// There are different kinds of structs. For more information, take a look at the
76
- /// [Rust Book][book].
135
+ /// Another shortcut for struct instantiation is available when you need to make a new struct that
136
+ /// shares most of a previous struct's values called struct update syntax:
137
+ ///
138
+ /// ```rust
139
+ /// # struct Foo { field1: String, field2: () }
140
+ /// # let thing = Foo { field1: "".to_string(), field2: () };
141
+ /// let updated_thing = Foo {
142
+ /// field1: "a new value".to_string(),
143
+ /// ..thing
144
+ /// };
145
+ /// ```
77
146
///
147
+ /// Tuple structs are instantiated in the same way as tuples themselves, except with the struct's
148
+ /// name as a prefix: `Foo(123, false, 0.1)`.
149
+ ///
150
+ /// Empty structs are instantiated with just their name and nothing else. `let thing =
151
+ /// EmptyStruct;`
152
+ ///
153
+ ///
154
+ /// # Style conventions
155
+ ///
156
+ /// Structs are always written in CamelCase, with few exceptions. While the trailing comma on a
157
+ /// struct's list of fields can be omitted, it's usually kept for convenience in adding and
158
+ /// removing fields down the line.
159
+ ///
160
+ /// For more information on structs, take a look at the [Rust Book][book] or the
161
+ /// [Reference][reference].
162
+ ///
163
+ /// [`PhantomData`]: marker/struct.PhantomData.html
78
164
/// [book]: https://doc.rust-lang.org/book/second-edition/ch05-01-defining-structs.html
165
+ /// [reference]: https://doc.rust-lang.org/reference/items/structs.html
166
+
79
167
mod struct_keyword { }
0 commit comments