Skip to content

Commit

Permalink
Update compound-types.md (#139)
Browse files Browse the repository at this point in the history
* Update compound-types.md

More details about struct field orders, and enums having tags.

Co-authored-by: Miguel Raz Guzmán Macedo <[email protected]>
  • Loading branch information
jonathanpallant and miguelraz committed Jan 13, 2025
1 parent a36f1ac commit 4e6b410
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions training-slides/src/compound-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 as 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 think of this as being a `struct` containing an `int`
and a `union`.

## Doing a `match` on an `enum`

Expand Down

0 comments on commit 4e6b410

Please sign in to comment.