Table of Contents

What are structs?

Structs are custom data types that allow developers to group together multiple variables under one name. Unlike arrays, which store elements of the same type, structs can hold multiple types of data in a single structure. 

Structs are useful for representing complex data entities, such as user profiles, transactions, orders, and metadata, making it easier to manage and organize data within a program.

General structure of structs

A struct is composed of fields, each with its own data type and name. The key components are:

  • Fields: A struct contains multiple fields, each holding a specific piece of data, like a name or an age. Fields can be of different types, including integers, strings, and addresses.
  • Custom data type: A struct defines a custom type, which can be used throughout the contract wherever this specific combination of data is needed.

The code block below shows a Person struct with three fields: name, age, and wallet. Each field has a specific data type that determines the kind of data it can store.

/ Struct Name
struct Person {
    // Fields and Data Types
    string name; // Data Type: String | Field: name
    uint256 age; // Data Type: Unsigned Integer (256 bits) | Field: age
    address wallet; // Data Type: Ethereum Address | Field: wallet
}

Initialize the struct: A new Person instance is created using the new keyword and its fields are initialized.

// Create a new Person instance using the `new` keyword and initialize its fields
Person memory person = new Person({
    name: "John Doe",
    age: 30,
    address: 0x1234567890123456
});

Accessing fields: Fields in a struct are accessed using dot notation (e.g., person.name).

//Accessing fields using dot notation 
string name = person.name; // "John Doe" 
uint256 age = person.age; // 25 
address wallet = person.wallet; // 0x1234567890123456

A visual representation of the struct

A visual representation of the Person struct based on the code block above.

What languages use structs?

Structs (or similar structures) are widely used across traditional programming languages like C, C++, and Python (classes) and in smart contract development languages. 

Structs in smart contract development

  1. Solidity (Ethereum smart contracts)
  2. Vyper (Ethereum smart contracts)
  3. Rust (Polkadot/Substrate smart contracts)
  4. Move (Aptos and Diem blockchains) 
  5. Cairo (Starknet smart contracts)

Each language has its own syntax for defining structs, but the core concept remains the same. 

What are the advantages of structs?

  • Data organization: Structs allow developers to logically group related data fields, making code more readable and maintainable.
  • Custom data types: Structs create custom types that simplify management of complex data without having to create multiple variables for each field.
  • Efficiency: In blockchain environments, where gas costs are a concern, structs help reduce redundancy and improve gas efficiency by grouping data into a single unit.

What kinds of data are stored in structs?

Structs can store a wide range of data types, including addresses, integers, strings, and more. The specific fields included in a struct depend on the contract's use case. 

User profiles (Solidity)

struct UserProfile {
    string username;
    address userAddress;
    uint256 reputation;
}

Token metadata (Move)

public struct TokenMetadata {
    token_name: vector<u8>,
    token_id: u64,
    token_symbol: vector<u8>,
 } has copy, drop, store

Transaction data (Rust)

struct Transaction {
    tx_id: u32,
    sender: String,
    receiver: String ,
}

Voting ballots (Vyper)

struct Ballot {
    voterAddress: address
    voteWeight: uint256
    hasVoted: bool
}

Employee Records (Cairo)

struct Employee {
    age: u8,
    id: u32,
    role: felt252,
}

If you’re interested in learning how to build structs in Solidity, Cyfrin Updraft can teach you how!

Related Terms

No items found.