Aptos

2025年09月09日更新 4 人订阅
原价: ¥ 2.2 限时优惠
专栏简介 Aptos 开发实战:从环境搭建到第一个 Hello World Aptos 开发指南:在 JetBrains 编辑器中配置运行、编译、测试与发布部署,实现更高效开发 Aptos 区块链智能合约入门:使用 Move 实现消息存储与检索 Aptos Move 语言中的变量管理与内存所有权机制详解 Aptos Move 编程语言中的四大基础类型解析:UINT、STRING、BOOL 与 ADDRESS 深入解读 APTOS-MOVE 中的 Vector 向量核心特性与操作 深入理解APTOS-MOVE中的函数修饰符:核心概念与应用 深入解读 Aptos Move 的 Struct 特性与四大能力 Aptos Move 控制流解析:IF、WHILE与LOOP的深入解读 Aptos Move 模块的特性与实操指南:模块引用与作用域管理 Aptos Move 模块的发布与交互:完整指南 深入理解 Aptos Move 中的 Object 创建与管理 深入探索 Aptos Move:Object 配置与实操指南 使用 Aptos Move 实现随机数生成:从 AIP-41 到实战操作 Aptos Move 实践指南:构建并部署同质化代币水龙头 (FA Faucet) Aptos Move NFT 项目实操指南:从开发到部署全流程解析 Aptos Move 开发入门:从环境搭建到合约部署全流程实录 Aptos Move 入门:从零到一的合约开发与测试实战 Move 语言核心:布尔逻辑与地址类型的实战精解 深入 Aptos Move:从public到friend,函数可见性详解 Aptos Move 编程:for、while 与 loop 循环的实战详解 Aptos Move 安全编程:abort 与 assert! 错误处理实战 Aptos Move 实战:基础运算与比较逻辑的实现与测试 Aptos Move 性能优化:位运算与移位操作实战 Aptos Move 实战:as 关键字与整数类型转换技巧 Aptos Move DeFi 实战:从零构建流动性池兑换逻辑 Aptos Move 实战:用 signer 实现合约所有权与访问控制 Aptos Move 核心安全:& 与 &mut 引用机制详解 Aptos Move 实战:全面掌握 SimpleMap 的增删改查 Aptos Move 入门:掌握链上资源(Resource)的增删改查 Aptos Move 深度实践:用嵌套数据结构构建链上金银储备系统 Aptos Move 实操:如何用 Tables 构建一个链上房产管理系统

深入解读 Aptos Move 的 Struct 特性与四大能力

深入解读AptosMove的Struct特性与四大能力在AptosMove编程语言中,struct是一种用于存储和组织数据的核心结构。它不仅可以嵌套使用,还能赋予特定的能力,从而增强其在不同场景中的使用效率。在本文中,我们将详细讲解AptosMove中的struct特性

深入解读 Aptos Move 的 Struct 特性与四大能力

在 Aptos Move 编程语言中,struct 是一种用于存储和组织数据的核心结构。它不仅可以嵌套使用,还能赋予特定的能力,从而增强其在不同场景中的使用效率。在本文中,我们将详细讲解 Aptos Move 中的 struct 特性,以及它们如何利用四种关键能力:copy、drop、store 和 key 来实现更强的功能。

本文对 Aptos Move 编程语言中的 struct 结构体及其特性进行了深入解读,重点介绍了 struct 的四大能力:copy(复制)、drop(销毁)、store(全局存储)和 key(全局存储键)。我们通过代码示例详细演示了这些能力如何影响 struct 的行为,特别是在全局存储、资源管理和复制操作中的应用。文章还提供了使用这些能力进行数据操作的实际用例,帮助开发者更好地理解并在项目中灵活运用 Aptos Move 的强大特性。

APTOS-MOVE STRUCT 特性解读

核心概念

Aptos Move 的 Struct 结构体用于存储结构化数据。Struct 可以相互嵌套,并可以作为资源存储在特定地址下。”

  1. 命名必须以大写字母开头
  2. 可以通过 has 关键字赋予能力
struct Person {
  ape: u8,
  birthday: u32,
}

struct People {
  people: vector<Person>,
}

修饰符与四大能力

Copy - 值能够被复制

Drop - 值可以在作用域结束时被自动清理

Key - 值可以用作于全局存储的键 Key

Store - 值可以存储在全局存储中

除 Struct 类型外,其他的类型默认具备 store、drop、copy 的能力,Struct 最终是存储在用户的地址上(或者被销毁),不存在 Aptos 合约里,Aptos 合约是一个全纯的函数。

The Four Abilities

The four abilities are:

  • copy
    • Allows values of types with this ability to be copied.
  • drop
    • Allows values of types with this ability to be popped/dropped.
  • store
    • Allows values of types with this ability to exist inside a struct in global storage.
  • key
    • Allows the type to serve as a key for global storage operations.

1. Drop

值能够在使用结束后被销毁

//drop

struct Coin {

  b: bool

}

#[test]
fun test6() {
  let c = Coin { b: true }; // 报错,没有 drop 修饰符
}

//drop
struct Coin has drop {

  b: bool,
}

#[test]
fun test6() {
  let c = Coin { b: true }; // 正常运行,拥有 drop 能力
}

2. copy

值能够在使用结束后被复制

//copy

struct CanCopy has drop {

  b: bool,

}

#[test]

fun test5() {

  let c = CanCopy { b: true };

  let c1 = c; // no copy

  let CanCopy { b } = &mut c1;

  *b = false;

  debug::print(&c1.b);

}

修改之后

// copy
struct CanCopy has copy, drop {
  b: bool,
}

#[test]
fun test5() {
  let c = CanCopy { b: true };
  let c1 = c; // copy
  let CanCopy { b } = &mut c1;
  *b = false;
  debug::print(&c1.b);
  debug::print(&c.b);
}

3. Key

值可以用作于全局存储的键 Key

//key
struct Coin has key {
  value: u64,
}

public entry fun mint(account: &signer, value: u64) {
  move_to(account, Coin { value });
}

#[test(account = @0x42)]
public fun test_mint(account: &signer) acquires Coin {
  let addr = signer::address_of(account);
  mint(account, 100);
  let coin = borrow_global<Coin>(addr).value;
  debug::print(&coin);
}

4. store

值可以被全局存储,通常配合Key使用

//store

struct Key has key, drop {

  a: Store,

}

struct Store has drop {

  b: bool,

}

#[test]
fun test6() {
  let k = Key {
​    a: Store { b: true },
  };

  debug::print(&k.a.b); // 会报错
}

父 的有 Key 子的必须有 Store

//store
struct Key has key, drop {
  a: Store,
}

struct Store has store, drop {
  b: bool,
}

#[test]
fun test6() {
  let k = Key {
    a: Store { b: true },
  };
  debug::print(&k.a.b);
}

实操

module 0x42::Demo2 {
    use std::debug;
    use std::signer;

    // drop
    struct Foo has drop {
        u: u64,
        b: bool
    }

    #[test]
    fun test() {
        let f = Foo { u: 42, b: true };
        // let Foo { u, b } = f;
        // debug::print(&u);
        // debug::print(&b);

        debug::print(&f.u);
        debug::print(&f.b);
    }

    #[test]
    fun test2() {
        let f = Foo { u: 42, b: true };
        let Foo { u, b } = f;
        debug::print(&u);
        debug::print(&b);
    }

    #[test]
    fun test3() {
        let f = Foo { u: 42, b: true };
        let Foo { u, b } = &mut f;
        *u = 43;
        debug::print(&f.u);
        debug::print(&f.b);
    }

    // copy
    struct CanCopy has copy, drop {
        u: u64,
        b: bool
    }

    #[test]
    fun test4() {
        let b1 = CanCopy { u: 42, b: true };
        let b2 = copy b1;
        let CanCopy { u, b } = &mut b2;
        *u = 43;
        debug::print(&b1.u);
        debug::print(&b2.u);

    }

    // store
    struct Key has key, drop {
        s: Struct
    }

    struct Struct has store, drop {
        x: u64
    }

    #[test]
    fun test5() {
        let k = Key { s: Struct { x: 42 } };
        debug::print(&k.s.x);
        let Struct { x } = &mut k.s;
        *x = 43;
        debug::print(&k.s.x);
    }
}

运行测试

image-20240912115452451.png

Aptos Move 中的 struct 是一个强大的工具,结合四大能力,可以极大地增强数据管理的灵活性与安全性。理解并灵活运用这些能力,是编写高效、安全智能合约的基础。在未来的开发中,这些能力将为资源管理和全局存储提供更多可能性。

参考

点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论