Skip to content

Commit

Permalink
Add empty structs
Browse files Browse the repository at this point in the history
This should be quick, but introduces the syntax and the concept of a
ZST.
  • Loading branch information
djmitche committed Jan 19, 2025
1 parent 68e1ebd commit d262330
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
- [User-Defined Types](user-defined-types.md)
- [Named Structs](user-defined-types/named-structs.md)
- [Tuple Structs](user-defined-types/tuple-structs.md)
- [Empty Structs](user-defined-types/empty-structs.md)
- [Enums](user-defined-types/enums.md)
- [Type Aliases](user-defined-types/aliases.md)
- [Const](user-defined-types/const.md)
Expand Down
27 changes: 27 additions & 0 deletions src/user-defined-types/empty-structs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
minutes: 2
---

# Empty Structs

Sometimes we just need a type, without any data. The only value of such a type
has the same name as the type. This is a zero-sized type (ZST).

```rust,editable
struct NullMessageDestination;
fn main() {
let _message_destination = NullMessageDestination;
// ...
}
```

ZSTs will be useful when we talk about traits and generics on day 2.

<details>

- This is a shorthand for a tuple struct with zero tuple elements.
- There isn't much to see here, yet, but students might imagine a trivial
implementation of an interface may have no data to store.

</details>
4 changes: 0 additions & 4 deletions src/user-defined-types/tuple-structs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
minutes: 10
---

<!-- NOTES:
Tuple structs, newtype wrappers, unit-like structs, including initialization syntax
-->

# Tuple Structs

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

0 comments on commit d262330

Please sign in to comment.