Structs let you group several named fields into one custom type, and enums let you define a type with a small fixed set of named values. In Solidity you use a struct to model a record such as a user or an order, and an enum to model a state such as Pending, Shipped or Delivered. The solidity struct enum pair is the main tool for modelling your own data in a contract.
This guide uses Solidity 0.8.x and assumes you already know mappings and arrays.
A struct is declared inside or outside a contract with the struct keyword and a list of typed members.
There are two ways to build a struct value. The named form Book({title: title, ...}) is clearer because the order of fields does not matter. The positional form Book(title, msg.sender, year, true) must follow the declaration order exactly. Omitted fields are not allowed in either form.
A struct held in a storage array or mapping is changed through a storage pointer, so the original is updated and no copy is made.
Using Book memory b = books[index] here would only copy the book, and your update would be lost. The automatic getter of a public struct array returns the fields as separate values, not as one struct, and it skips members that are arrays or mappings.
The most common pattern stores records by key with a mapping of structs.
Because a mapping returns a zeroed struct for an unknown key, the exists flag is how you tell a real record from an empty default. A struct can contain a mapping or an array, but a struct cannot contain itself directly; it can hold a dynamic array or mapping of itself instead.
An enum defines named constants. Internally each one is an integer starting at 0, and an enum may have between 1 and 256 members.
The default value of an enum variable is its first member, so put the safest or neutral state first. Enums make state machines readable: the checks above show which transitions are legal. Converting an integer to an enum with Status(n) reverts if n is out of range, which protects you from invalid states. Use type(Status).max to get the highest member value.
Structs often include an enum member, which gives each record its own state.
uint128 and bool, into a single storage slot.calldata for struct arguments you only read, and memory when you must change them.uint8, so off-chain code sees a number. Document the numbering.The first member, with integer value 0. Order your members so that this default is a safe state.
Yes. Return it as memory, and the ABI encodes it as a tuple. Front-end libraries decode it into an object.
Not directly. You can loop from 0 to uint256(type(Status).max) and convert each integer with Status(i).
Yes, but such a struct can only live in storage and cannot be copied to memory.
Use a struct to bundle related fields into one type and an enum to name a small set of states. Create structs with the named or positional form, edit stored ones through storage pointers, and use an exists flag to detect empty mapping entries. Enums start at their first member, make state checks readable and revert on invalid conversions.