Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d262330

Browse files
committedJan 19, 2025·
Add empty structs
This should be quick, but introduces the syntax and the concept of a ZST.
1 parent 68e1ebd commit d262330

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed
 

‎src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
- [User-Defined Types](user-defined-types.md)
6565
- [Named Structs](user-defined-types/named-structs.md)
6666
- [Tuple Structs](user-defined-types/tuple-structs.md)
67+
- [Empty Structs](user-defined-types/empty-structs.md)
6768
- [Enums](user-defined-types/enums.md)
6869
- [Type Aliases](user-defined-types/aliases.md)
6970
- [Const](user-defined-types/const.md)
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
minutes: 2
3+
---
4+
5+
# Empty Structs
6+
7+
Sometimes we just need a type, without any data. The only value of such a type
8+
has the same name as the type. This is a zero-sized type (ZST).
9+
10+
```rust,editable
11+
struct NullMessageDestination;
12+
13+
fn main() {
14+
let _message_destination = NullMessageDestination;
15+
// ...
16+
}
17+
```
18+
19+
ZSTs will be useful when we talk about traits and generics on day 2.
20+
21+
<details>
22+
23+
- This is a shorthand for a tuple struct with zero tuple elements.
24+
- There isn't much to see here, yet, but students might imagine a trivial
25+
implementation of an interface may have no data to store.
26+
27+
</details>

‎src/user-defined-types/tuple-structs.md

-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
minutes: 10
33
---
44

5-
<!-- NOTES:
6-
Tuple structs, newtype wrappers, unit-like structs, including initialization syntax
7-
-->
8-
95
# Tuple Structs
106

117
If the field names are unimportant, you can use a tuple struct:

0 commit comments

Comments
 (0)
Please sign in to comment.