Quiz Entry - updated: 2026.07.30
What is a struct in Solidity, and how do you create one?
A struct defines a new custom type that groups several fields together — like defining your own composite type on top of the primitives.
struct Person {
uint256 favoriteNumber;
string name;
}
Person public person = Person({favoriteNumber: 2, name: "Patrick"});
// or positionally: Person(2, "Patrick");
Once declared, Person is a type you can use like uint256 or bool. You instantiate one either with named fields ({favoriteNumber: 2, name: "Patrick"}) or positionally (Person(2, "Patrick")), matching the declared order. The named form is more explicit and less error-prone. Structs are commonly combined with arrays or mappings to store many records. When a struct's getter returns its fields, they come back indexed by declaration order (field 0, field 1, …).
Go deeper:
Solidity docs: structs — declaring and using custom composite types.