Vyper theorytheory 0/50 · 0%
Data structures · easy

15. Structs

Grouping related fields into a named type.

A `struct` bundles related fields under one name, exactly as in Solidity. You construct an instance with keyword arguments.

struct User:
    name: String[32]
    age: uint256

users: public(HashMap[address, User])

@external
def set_user(name: String[32], age: uint256):
    self.users[msg.sender] = User({name: name, age: age})

Structs can live in storage, memory, or be passed as function arguments and return values, just like any other Vyper type.

Check your understanding

  1. 1. How do you construct a struct instance in Vyper?

  2. 2. Can a struct be stored in a HashMap value?

  3. 3. What keyword declares a struct type?