1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-03-22 14:59:37 +02:00

Add empty structs ()

This should be quick, but introduces the syntax and the concept of a
ZST.
This commit is contained in:
Dustin J. Mitchell 2025-01-22 11:45:01 -05:00 committed by GitHub
parent 7f712b5292
commit 5b03ea6ca5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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:
@ -52,6 +48,12 @@ fn main() {
- Rust generally doesn’t like inexplicit things, like automatic unwrapping or
for instance using booleans as integers.
- Operator overloading is discussed on Day 3 (generics).
- When a tuple struct has zero fields, the `()` can be omitted. The result is a
zero-sized type (ZST), of which there is only one value (the name of the
type).
- This is common for types that implement some behavior but have no data
(imagine a `NullReader` that implements some reader behavior by always
returning EOF).
- The example is a subtle reference to the
[Mars Climate Orbiter](https://en.wikipedia.org/wiki/Mars_Climate_Orbiter)
failure.