Views and Accessors

Struct fields are private to the module. To access them from outside the module, you must define an accessor function. The convention is to name the function after the field. It's up to the developer to decide which of the fields should be accessible from outside the module.

/// This module implements a simple object that represents a user record. All
/// of the fields of the record can be read by using the getter functions. The
/// email and phone fields can be changed by using the mutable access functions.
module examples::record {
    use std::string::String;
    use sui::tx_context::TxContext;
    use sui::object::{Self, UID};

    /// A single record of a user. An Object!
    struct UserRecord has key, store {
        id: UID,
        name: String,
        age: u8,
        email: String,
        phone: String,

    /// Creates a new user record
    public fun new(
        name: String, age: u8, email: String, phone: String, ctx: &mut TxContext
    ): UserRecord {
        UserRecord {
            id: object::new(ctx),
            name, age, email, phone,

    // === Mutable Accessors ===

    /// Return a mutable reference to the user's email.
    /// Hint: the `_mut` suffix on the function name marks this function as
    /// a mutable accessor.
    public fun email_mut(self: &mut UserRecord): &mut String { &mut }

    /// Return a mutable reference to the user's phone
    public fun phone_mut(self: &mut UserRecord): &mut String { &mut }

    // === Views ===

    /// Returns the user's name.
    /// Hint: view functions match property names.
    public fun name(self: &UserRecord): String { }

    /// Returns the user's age
    public fun age(self: &UserRecord): u8 { self.age }

    /// Returns the user's email
    public fun email(self: &UserRecord): String { }

    /// Returns the user's phone
    public fun phone(self: &UserRecord): String { }

This pattern is used in almost every application of Move.