From c00001f94e5a4f35ff2d95c0fd57f663af73680d Mon Sep 17 00:00:00 2001 From: Jonathan Pallant Date: Wed, 28 Feb 2024 10:56:48 +0000 Subject: [PATCH] Update compound-types.md More details about struct field orders, and enums having tags. --- training-slides/src/compound-types.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/training-slides/src/compound-types.md b/training-slides/src/compound-types.md index 4fba395e..9bee0b1d 100644 --- a/training-slides/src/compound-types.md +++ b/training-slides/src/compound-types.md @@ -13,6 +13,11 @@ struct Point { } ``` +Note: + +The fields may not be laid out in memory in the order they are written (unless +you ask the compiler to ensure that they are). + ## Construction - there is no partial initialization @@ -143,8 +148,17 @@ fn main() { ## Enums with Values -- An enum is the same size, no matter which variant is picked -- It will be the size of the largest variant +- An enum value is the same size, no matter which variant is picked +- It will be the size of the largest variant (plus a tag) + +Note: + +The tag in an enum specifies which variant is currently valid, and is stored the +smallest integer the compiler can get away with - it depends how many variants you +have. Of course, if none of the variants have any data, the enum is *just* the tag. + +If you have a C background, you can this of this as being a `struct` containing an `int` +and a `union`. ## Doing a `match` on an `enum`