From 5b03ea6ca559b9e28cc64e7805e89be013fb79aa Mon Sep 17 00:00:00 2001 From: "Dustin J. Mitchell" <djmitche@google.com> Date: Wed, 22 Jan 2025 11:45:01 -0500 Subject: [PATCH] Add empty structs (#2569) This should be quick, but introduces the syntax and the concept of a ZST. --- src/user-defined-types/tuple-structs.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/user-defined-types/tuple-structs.md b/src/user-defined-types/tuple-structs.md index 653e8741..53dd2b8f 100644 --- a/src/user-defined-types/tuple-structs.md +++ b/src/user-defined-types/tuple-structs.md @@ -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.