Skip to content

Commit 0e4629c

Browse files
committed
trpl/structs: Document unit-like structs
1 parent e2e678b commit 0e4629c

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/doc/trpl/structs.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ let origin = Point3d { x: 0, y: 0, z: 0 };
118118
let point = Point3d { z: 1, x: 2, .. origin };
119119
```
120120

121-
# Tuple Structs
121+
# Tuple structs
122122

123123
Rust has another data type that's like a hybrid between a [tuple][tuple] and a
124124
struct, called a ‘tuple struct’. Tuple structs have a name, but
@@ -177,3 +177,22 @@ println!("length is {} inches", integer_length);
177177
As you can see here, you can extract the inner integer type through a
178178
destructuring `let`, just as with regular tuples. In this case, the
179179
`let Inches(integer_length)` assigns `10` to `integer_length`.
180+
181+
# Unit-like structs
182+
183+
You can define a struct with no members at all:
184+
185+
```rust
186+
struct Electron;
187+
```
188+
189+
Such a struct is called ‘unit-like’ because it resembles the empty
190+
tuple, `()`, sometimes called ‘unit’. Like a tuple struct, it defines a
191+
new type.
192+
193+
This is rarely useful on its own (although sometimes it can serve as a
194+
marker type), but in combination with other features, it can become
195+
useful. For instance, a library may ask you to create a structure that
196+
implements a certain [trait][trait] to handle events. If you don’t have
197+
any data you need to store in the structure, you can just create a
198+
unit-like struct.

0 commit comments

Comments
 (0)