Join our
Discord!
LogoLogo
SupraScan ExplorerStarKey WalletDiscord
MoveVM
  • Network
  • Oracles
  • Automation
  • SupraNova
  • Guides
MoveVM
  • Overview
  • Getting Started
    • Install Supra CLI with Docker
    • Create a Supra Account
    • Create a Move Package
      • Initialize a Package
      • Write a Module
      • Compile and Publish
      • Interact with a Package
    • Create a dApp with StarKey
  • Network Information
  • Token Standards
  • Learn Move 101
    • Getting Started with Move
    • Unsigned Integers in Move
    • Math Operations in Move
    • Using Vectors in Move
    • Reading Resource Data with borrow_global
    • Passing Data in Move: Value vs. Reference
    • Adding Elements with vector::push_back
    • Emitting Events with event::emit
  • Move Book
    • Getting Started
      • Modules and Scripts
      • Move Tutorial
    • Primitive Types
      • Integers
      • Bool
      • Address
      • Vector
      • Signer
      • References
      • Tuples and Unit
    • Basic Concepts
      • Local Variables and Scope
      • Equality
      • Abort and Assert
      • Conditionals
      • While, For, and Loop
      • Functions
      • Structs and Resources
      • Constants
      • Generics
      • Type Abilities
      • Uses and Aliases
      • Friends
      • Packages
      • Package Upgrades
      • Unit Tests
    • Global Storage
      • Structure
      • Operators
    • Reference
      • Standard Library
      • Coding Conventions
  • TypeScript SDK
    • Guides
      • Create Supra Accounts
      • Publish a Package
    • Documentation
    • Repository
  • Rest API
    • Mainnet
      • Accounts
      • Faucet
      • Transactions
      • Block
      • View
      • Consensus
      • Events
      • Tables
    • Testnet
      • Accounts
      • Faucet
      • Transactions
      • Block
      • View
      • Events
      • Tables
  • Developer Resources
    • Supra Dapp Templates
    • Supra Move VS Code Extension
  • Links
    • Supra DevHub
    • SupraScan Block Explorer
    • StarKey Wallet
    • Live Data Feeds
    • Whitepapers
    • Security Audits
    • Supra's Official GitHub
Powered by GitBook
On this page
  • Vectors
  • Code Follows Like:
Edit on GitHub
  1. Learn Move 101

Using Vectors in Move

Vectors in Move are dynamic lists that can grow or shrink as needed, making them essential for managing collections of data, including structs.

PreviousMath Operations in MoveNextReading Resource Data with borrow_global

For the latest RPC URL, please refer to the page.

Vectors

When you want a list of values, use vectors. A vector in Move is dynamic by default and has no fixed size. It can get larger or smaller as needed. Vector in Supra and Aptos are available to import and use at std::vector.

You just need to do “use std::vector” at the top of your module to be able to access it.

You can also store structs in vectors, Note that for structs that are stored in a resource struct, you need to add the store attribute to the struct:

struct Balance has store {
        owner: address,
        balance: u64,
    }

struct GlobalData has key {
        balances: vector<Balance>,
    }

When creating an empty vector you can use the following syntax: let empty_vector = vector[];

We need to track all the Dinosaurs created from Dinosaur_nest, We can do this by declaring two new structs:

Dinosaur Struct having Key and a new resource struct named DinosaurSwarm which has a vector of Dinosaur structs, this resource needs to be stored at 0xcafe in init_module with an empty vector of Dinosaurs to start with.

Code Follows Like:

module 0xcafe::Dinosaur_nest {
   use std::vector;

    struct DinosaurGendna has key {
        Gendna_digits: u64,
        Gendna_modulus: u256,
    }

    struct Dinosaur has store {
        Gendna: u64,
    }
    
    struct DinosaurSwarm has store {
        Dinosaurs: vector<Dinosaur>,
    }

    fun init_module(cafe_signer: &signer) {
        let Gendna_digits = 10;
        let Gendna_modulus = 10 ^ Gendna_digits;
        move_to(cafe_signer, DinosaurGendna {
            Gendna_digits,
            Gendna_modulus: (Gendna_modulus as u256),
        });
    move_to(cafe_signer, DinosaurSwarm {
            Dinosaurs: vector[],
        });    
        
    }
}

We've only been using the init_module function, which is called when the module is deployed to initialize default values. we'll create one more function that will later be called by the user to create a new Dinosaur.

Let’s create a new function named spawn_Dinosaur that takes one argument of type u64 named Gendna and returns a Dinosaur struct with that Gendna:

Note: This function is public, which means it can be called from any other Move module. we'll keep all functions public, except for init_module. init_module has to be private because it's called only once when the module is deployed.

module 0xcafe::Dinosaur_nest {
    use std::vector;

    struct DinosaurGendna has key {
        Gendna_digits: u64,
        Gendna_modulus: u64,
    }

    struct Dinosaur has store {
        Gendna: u64,
    }

    struct DinosaurSwarm has key {
        Dinosaurs: vector<Dinosaur>,
    }

    fun init_module(cafe_signer: &signer) {
        let Gendna_digits = 10;
        let Gendna_modulus = 10 ^ Gendna_digits;
        move_to(cafe_signer, DinosaurGendna {
            Gendna_digits,
            Gendna_modulus: (Gendna_modulus as u256),
        });
        move_to(cafe_signer, DinosaurSwarm {
            Dinosaurs: vector[],
        });
    }
     
public fun spawn_Dinosaur(Gendna: u64): Dinosaur {
    Dinosaur {
            Gendna,
        } 
    }
}
Supra Network Information