• Move 学习记录


    Move在线IDE

    Move Playground

    基本数据类型

    只包括 无符号整型、布尔型、地址 3种类型,没有像其它语言中有字符串类型。

    数据类型数据类型表示符号
    无符号整型u8, u64, u128
    布尔类型bool
    地址类型address, ex: Std, 0x1, Sender

    变量定义(4种定义方式)

    1、let a = 10;

    2、let a:u8 = 10;

    3、let a = 10u8;

    4、let a: u8; a = 10;

    1. script {
    2. use 0x1::Debug;
    3. fun main() {
    4. // unsigned integer
    5. let a1 = 10;
    6. let a2: u8 = 255;
    7. let a3 = 65535u64;
    8. let a4: u128;
    9. a4 = 9223372036854775807;
    10. Debug::print(&a1);
    11. Debug::print(&a2);
    12. Debug::print(&a3);
    13. Debug::print(&a4);
    14. // bool type
    15. let b1 = true;
    16. let b2: bool = false;
    17. let b3: bool;
    18. b3 = true;
    19. Debug::print(&b1);
    20. Debug::print(&b2);
    21. Debug::print(&b3);
    22. // address type
    23. let c1 = @Std;
    24. let c2: address = @Std;
    25. let c3:address;
    26. c3 = @0x1;
    27. Debug::print(&c1);
    28. Debug::print(&c2);
    29. Debug::print(&c3);
    30. }
    31. }

    地址可以使用名称或地址值表示,如下图所示: 

    常量(全局)

    首字母必须是大写的A ~ Z,定义在module或script内

    const PI:u64 = 314;

    变量(局部)

    首字母必须是小写的a~z,定义在函数内

    let i = 1;

    函数

    script中只能有一个函数

    • 函数原型

    [public] fun function_name(param1:Type, ...): ReturnType {

            //function body

            [return] xx  //此行不加分号

    }

    • 函数说明

    (1)参数可以为0个或多个

    (2)返回值可以是0个或多个,多于1个返回值时,使用括号(), 如(u8, bool)

    (3)访问权限没有public时仅限于module内部访问

    (4)返回值表达式语句后不能加分号,可以不加return

    (5)注释不能使用中文

    1. module 0x1::Math{
    2. public fun sum(a:u8, b:u8) : u64 {
    3. (a as u64) + (b as u64) //return without semicolon
    4. }
    5. }
    6. script {
    7. use 0x1::Debug;
    8. use 0x1::Math;
    9. fun test_fun(a:u8, b:u8) {
    10. let result = Math::sum(a,b);
    11. Debug::print(&result);
    12. }
    13. }

    条件语句与循环语句

    注:条件语句if与循环语句while在{}后要加分号;

    1. module 0x1::Math{
    2. public fun sum(count: u64) : u64 {
    3. let i = 0;
    4. let result = 0;
    5. while (i <= count) {
    6. i = i + 1;
    7. if ( i % 2 == 0) continue;
    8. result = result + i;
    9. };
    10. return result
    11. }
    12. }

    abort和assert

    (1)abort

    abort 0;   //终止执行,恢复事务

    1. script {
    2. use Std::Debug;
    3. fun test_abort() {
    4. let age = 15;
    5. Debug::print(&age);
    6. abort 0;
    7. Debug::print(&age);
    8. }
    9. }

    输出结果:

    debug: 15

    Execution aborted with code 0 in transaction script

    (2)assert 

    assert!(条件表达式, 错误码);  //封装了abort,条件不满足时执行 

    1. script {
    2. use Std::Debug;
    3. fun test_assert() {
    4. let age = 15;
    5. Debug::print(&age);
    6. assert!(age == 20, 1001);
    7. Debug::print(&age);
    8. }
    9. }

    输出结果:

    debug: 15

    Execution aborted with code 1001 in transaction script

    引用

    &mut 可变引用, 变量可修改

    & 不可变引用,即变量不可修改

    1. module Std::Utils{
    2. public fun swap(a:&mut u8, b:&mut u8) {
    3. let temp = *a;
    4. *a = *b;
    5. *b = temp;
    6. }
    7. }
    8. script {
    9. use Std::Debug;
    10. use Std::Utils;
    11. fun test_swap() {
    12. let (a, b) = (2,5);
    13. Debug::print(&a);
    14. Debug::print(&b);
    15. Utils::swap(&mut a, &mut b);
    16. Debug::print(&a);
    17. Debug::print(&b);
    18. }
    19. }

    泛型

    fun function_name(param: T,...)

    1. module 0x1::Utils{
    2. use 0x1::Debug;
    3. public fun print(a: T) {
    4. Debug::print(&a);
    5. }
    6. }
    7. script {
    8. use 0x1::Utils;
    9. fun test_generic() {
    10. let a = 10u8;
    11. Utils::print(a);
    12. Utils::print(a);
    13. let b = true;
    14. Utils::print(b);
    15. let c = @Std;
    16. Utils::print(c);
    17. }
    18. }

    输出结果:

    debug: 10

    debug: 10

    debug: true

    debug: 0000000000000000000000000000000000000000000000000000000000000001

    Gas used: 18

    Vector(泛型容器)

    1. script {
    2. use 0x1::Utils;
    3. use 0x1::Vector;
    4. fun test_vector() {
    5. let a = b"hello, world";
    6. Utils::print(a);
    7. let v = Vector::empty();
    8. Vector::push_back(&mut v, 10);
    9. Vector::push_back(&mut v, 20);
    10. Vector::push_back(&mut v, 30);
    11. Vector::push_back(&mut v, 40);
    12. Vector::push_back(&mut v, 50);
    13. Utils::print(v);
    14. Vector::pop_back(&mut v);
    15. Utils::print(v);
    16. Vector::remove(&mut v, 2);
    17. Utils::print(v);
    18. Vector::swap(&mut v, 0, 1);
    19. Utils::print(v);
    20. Vector::swap_remove(&mut v, 0);
    21. Utils::print(v);
    22. let b = 10;
    23. let (flag, index) = Vector::index_of(&v, &b);
    24. Utils::print(flag);
    25. Utils::print(index);
    26. b = 20;
    27. let isExist = Vector::contains(&v, &b);
    28. Utils::print(isExist);
    29. Vector::reverse(&mut v);
    30. Utils::print(v);
    31. let c = Vector::borrow(&v, 0);
    32. //*c = 90; //inmutable
    33. Utils::print(*c);
    34. let d = Vector::borrow_mut(&mut v, 0);
    35. *d = 90;
    36. Utils::print(*d);
    37. let v2 = Vector::empty();
    38. Vector::append(&mut v2, v);
    39. Utils::print(v2);
    40. let count = Vector::length(&v2);
    41. Utils::print(count);
    42. }
    43. }

    输出结果:

    debug: (&) [104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100]

    debug: (&) [10, 20, 30, 40, 50]

    debug: (&) [10, 20, 30, 40]

    debug: (&) [10, 20, 40]

    debug: (&) [20, 10, 40]

    debug: (&) [40, 10]

    debug: true

    debug: 1

    debug: false

    debug: (&) [10, 40]

    debug: 10

    debug: 90

    debug: (&) [90, 40]

    debug: 2

    Gas used: 195

    Move类型的能力

    copy 可被复制(基本类型自带此能力)

    drop 可被丢弃(基本类型自带此能力)

    key 可作为键值

    store 可存储到全局状态(基本类型自带此能力)

    自定义结构(struct)

    定义:

    struct Stuct_name [ has ability ] {

            field_name: type,

            ...

            field_name: type

    }

    说明:

    结构体名称首字母大写

    字段数量在0~65535

    结构体不可递归

    1. address 0x1{
    2. module person {
    3. struct Person has drop, copy {
    4. id: u64,
    5. age: u8,
    6. sex: bool //without comma
    7. }
    8. public fun new_person(_id:u64, _age:u8, _sex:bool) : Person {
    9. return Person {
    10. id: _id,
    11. age: _age,
    12. sex: _sex, //with comma
    13. }
    14. }
    15. public fun getId(p: Person) : u64 {
    16. return p.id
    17. }
    18. public fun getAge(p: Person) : u8 {
    19. return p.age
    20. }
    21. public fun getSex(p: Person) : bool {
    22. return p.sex
    23. }
    24. }
    25. }
    26. script {
    27. use 0x1::Debug;
    28. use 0x1::person;
    29. fun test_struct(){
    30. let p = person::new_person(110111, 20, true);
    31. let id = person::getId(p);
    32. let age = person::getAge(p);
    33. let sex = person::getSex(p);
    34. Debug::print(&id);
    35. Debug::print(&age);
    36. Debug::print(&sex);
    37. }
    38. }

    输出结果:

    debug: 110111

    debug: 20

    debug: true

    Gas used: 21

    泛型结构

    Move所有权

    每个变量都有所有权,所有权的属主是作用域

    • move 属主转移
    • copy 拷贝值
    1. module 0x1::Utils{
    2. use 0x1::Debug;
    3. //Generic
    4. public fun print(a: T) {
    5. Debug::print(&a);
    6. }
    7. }
    8. script {
    9. use 0x1::Utils;
    10. fun test_ownership() {
    11. let a = 5;
    12. Utils::print(copy a);
    13. Utils::print(move a);
    14. //Utils::print(a);
    15. }
    16. }

    Signer(发送交易者)

    Signer是原生数据类型,只有 一种能力drop,即类似于如下结构:

    struct Signer has drop {

            addr: address

    }

    Signer API

    • address_of(&Signer):address //返回Signer地址
    • borrow_address(&Signer) : &address //返回Signer地址的引用
    1. script {
    2. use 0x1::Signer;
    3. use 0x1::Debug;
    4. fun test_signer(sig: signer) {
    5. let addr = Signer::address_of(&sig);
    6. Debug::print(&addr);
    7. let addr_ref = Signer::borrow_address(&sig);
    8. Debug::print(addr_ref);
    9. }
    10. }

    运行输入:

    test_signer(0x55)

    输出结果: 

    debug: 0000000000000000000000000000000000000000000000000000000000000055

    debug: 0000000000000000000000000000000000000000000000000000000000000055

    Gas used: 13

    Resource(资源)

    资源是被限制了能力的结构,只有key与store

    资源必须存储在账户下,一个账户同一时刻只能容纳一个资源

    Resource API 

    move_to(&Signer, T)                                //将资源发布给Signer

    move_from(address): T                            //删除Signer的T资源并返回

    borrow_global_mut(address):&mut T       //返回Signer下T的可变引用

    borrow_global(address): &T                     //返回Signer下的不可变引用

    exists(adress): bool                                  //返回address下的T

     

    1. module 0x1::collection {
    2. use 0x1::Vector;
    3. use 0x1::Signer;
    4. struct Item has store, drop {}
    5. struct Collection has store, key {
    6. items: vector,
    7. }
    8. public fun create_resource(sig: &signer) {
    9. move_to(sig, Collection{
    10. items: Vector::empty()
    11. })
    12. }
    13. public fun is_exist_resource(addr: address) : bool {
    14. exists(addr)
    15. }
    16. public fun add_resource(sig : &signer) acquires Collection {
    17. let addr = Signer::address_of(sig);
    18. let collection = borrow_global_mut(addr);
    19. Vector::push_back(&mut collection.items, Item{});
    20. }
    21. public fun size_resource(sig: &signer): u64 acquires Collection {
    22. let addr = Signer::address_of(sig);
    23. let collection = borrow_global(addr);
    24. Vector::length(&collection.items)
    25. }
    26. public fun destory_resource(sig: &signer) acquires Collection {
    27. let addr = Signer::address_of(sig);
    28. let collectio = move_from(addr);
    29. let Collection{items: _} = collectio;
    30. }
    31. }
    32. script {
    33. use 0x1::Debug;
    34. use 0x1::Signer;
    35. use 0x1::collection as col;
    36. fun test_resource(sig: signer) {
    37. let addr = Signer::address_of(&sig);
    38. let isExist = col::is_exist_resource(addr);
    39. Debug::print(&isExist);
    40. if (isExist) {
    41. col::destory_resource(&sig);
    42. };
    43. col::create_resource(&sig);
    44. col::add_resource(&sig);
    45. let size = col::size_resource(&sig);
    46. Debug::print(&size);
    47. }
    48. }

  • 相关阅读:
    unity 烘焙的时候出现模型没有光影的情况
    Java进行多线程编程?(lambda表达式~)
    535、RabbitMQ详细入门教程系列 -【消息与队列进阶 RabbitMQ(一)】 2022.07.29
    【Hadoop】-Hive客户端:HiveServer2 & Beeline 与DataGrip & DBeaver[14]
    秋招面经第八弹:网易二面-数据开发工程师
    第二十三章《斗地主游戏》第3节:项目完整代码
    Vue echarts 饼图 引导线加小圆点,文字分行展示
    jquery+ajax验证不通过也提交表单问题处理
    vue3 antd 表单校验与重置
    PTA:数据结构实验 一 链队列
  • 原文地址:https://blog.csdn.net/ling1998/article/details/127784208