🎓 A1Learner
🔍
0 of 50 lessons completed

Solidity Structs

A struct is a custom type that groups named fields into one value. It lets related pieces of data travel together as a single item.

Make a struct and read its fields

The Book struct groups a title, a page count, and a read flag. The variable fills the fields in order, and small functions read them back.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract Library {
    struct Book {
        string title;
        uint256 pages;
        bool read;
    }

    Book public book = Book("Dune", 412, false);

    function getTitle() public view returns (string memory) {
        return book.title;
    }

    function getPages() public view returns (uint256) {
        return book.pages;
    }

    function isRead() public view returns (bool) {
        return book.read;
    }
}

Change one field

Assigning to book.read changes only that field. The page count stays exactly as it was.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract ReadingList {
    struct Book {
        string title;
        uint256 pages;
        bool read;
    }

    Book public book = Book("Dune", 412, false);

    function markRead() public {
        book.read = true;
    }

    function getPages() public view returns (uint256) {
        return book.pages;
    }

    function isRead() public view returns (bool) {
        return book.read;
    }
}

The public getter returns a tuple

A public struct variable grows a reading function like other public variables. That function returns every field at once, in declaration order.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract BookRecord {
    struct Book {
        string title;
        uint256 pages;
        bool read;
    }

    Book public book = Book("Emma", 474, true);
}

Example

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract PersonalLibrary {
    struct Book {
        string title;
        uint256 pages;
        bool read;
    }

    Book public favourite = Book("The Hobbit", 310, false);

    function finishBook() public {
        favourite.read = true;
    }

    function getTitle() public view returns (string memory) {
        return favourite.title;
    }

    function getPages() public view returns (uint256) {
        return favourite.pages;
    }

    function isRead() public view returns (bool) {
        return favourite.read;
    }
}

Key points

  • Struct: a custom type built from named fields, each with its own type.
  • Field: one named piece of data inside a struct.
  • Creation: Book("Dune", 412, false) fills the fields in the order they are declared.
  • Field access: a dot followed by the field name reads that one field, as in book.pages.
  • Changing a field: assigning to one field changes only that field and leaves the others alone.
  • Public getter: the reading function of a public struct variable returns all fields together as a tuple.

In this example, the PersonalLibrary contract declares a Book struct with a title, a page count, and a read flag. It creates the favourite book in field order, starting unread. The getTitle, getPages, and isRead functions read one field each. The finishBook function changes only the read field, so the book ends read while its page count stays 310.

Note

a struct is a shape only, so it holds no data until you create a value of that shape.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Remix IDE

Exercise

What does Book("Dune", 412, false) do?