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;

script { 
    use 0x1::Debug; 
 
    fun main() { 
        // unsigned integer
        let a1 = 10; 
        let a2: u8 = 255; 
        let a3 = 65535u64; 
        let a4: u128; 
        a4 = 9223372036854775807;
 
        Debug::print(&a1); 
        Debug::print(&a2); 
        Debug::print(&a3); 
        Debug::print(&a4);

        // bool type
        let b1 = true; 
        let b2: bool = false;
        let b3: bool;
        b3 = true;

        Debug::print(&b1);
        Debug::print(&b2);
        Debug::print(&b3);

        // address type
        let c1 = @Std;
        let c2: address = @Std;
        let c3:address;
        c3 = @0x1;

        Debug::print(&c1);
        Debug::print(&c2);
        Debug::print(&c3);
    } 
}

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

常量(全局)

首字母必须是大写的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)注释不能使用中文

module 0x1::Math{   
    public fun sum(a:u8, b:u8) : u64 {
        (a as u64) + (b as u64) //return without semicolon
    }
}

script {
    use 0x1::Debug;
    use 0x1::Math;

    fun test_fun(a:u8, b:u8) {
        let result = Math::sum(a,b);
        Debug::print(&result);    
    }
}

条件语句与循环语句

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

module 0x1::Math{ 
    public fun sum(count: u64) : u64 { 
        let i = 0; 
        let result = 0; 
        while (i <= count) { 
            i = i + 1; 
            if ( i % 2 == 0) continue;
            result = result + i; 
        }; 
        return result 
    } 
}

abort和assert

(1)abort

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

script { 
    use Std::Debug; 
 
    fun test_abort() { 
        let age = 15; 
        Debug::print(&age); 
 
        abort 0; 
        Debug::print(&age); 
    } 
}

输出结果:

debug: 15

Execution aborted with code 0 in transaction script

(2)assert 

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

script {
    use Std::Debug;

    fun test_assert() {
        let age = 15;
        Debug::print(&age);

        assert!(age == 20, 1001); 
        Debug::print(&age);
    }
}

输出结果:

debug: 15

Execution aborted with code 1001 in transaction script

引用

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

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

module Std::Utils{
    public fun swap(a:&mut u8, b:&mut u8) {
        let temp = *a;
        *a = *b;
        *b = temp;
    }
}

script {
    use Std::Debug;
    use Std::Utils;

    fun test_swap() {
        let (a, b) = (2,5);
        Debug::print(&a);
        Debug::print(&b);

        Utils::swap(&mut a, &mut b);
        
        Debug::print(&a);       
        Debug::print(&b);
    }
}

泛型

fun function_name<T>(param: T,...)

module 0x1::Utils{ 
    use 0x1::Debug; 

    public fun print<T:drop>(a: T) { 
        Debug::print(&a); 
    } 
}

script {
    use 0x1::Utils;

    fun test_generic() { 
        let a = 10u8; 
        Utils::print<u8>(a); 
        Utils::print(a);
 
        let b = true; 
        Utils::print(b); 
 
        let c = @Std; 
        Utils::print(c);   
    } 
}

输出结果:

debug: 10

debug: 10

debug: true

debug: 0000000000000000000000000000000000000000000000000000000000000001

Gas used: 18

Vector(泛型容器)

script {
    use 0x1::Utils;
    use 0x1::Vector;

    fun test_vector() {
        let a = b"hello, world";
        Utils::print(a);

        let v = Vector::empty<u8>();
        Vector::push_back<u8>(&mut v, 10);
        Vector::push_back(&mut v, 20);
        Vector::push_back(&mut v, 30);
        Vector::push_back(&mut v, 40);
        Vector::push_back(&mut v, 50);
        Utils::print(v);

        Vector::pop_back(&mut v);
        Utils::print(v);

        Vector::remove(&mut v, 2);
        Utils::print(v);

        Vector::swap(&mut v, 0, 1);
        Utils::print(v);

        Vector::swap_remove(&mut v, 0);
        Utils::print(v);

        let b = 10;
        let (flag, index) = Vector::index_of<u8>(&v, &b);
        Utils::print(flag);
        Utils::print(index);

        b = 20;
        let isExist = Vector::contains(&v, &b);
        Utils::print(isExist);

        Vector::reverse(&mut v);
        Utils::print(v);

        let c = Vector::borrow(&v, 0);
        //*c = 90;  //inmutable
        Utils::print(*c);

        let d = Vector::borrow_mut(&mut v, 0);
        *d = 90;
        Utils::print(*d);

        let v2 = Vector::empty<u8>();
        Vector::append<u8>(&mut v2, v);
        Utils::print(v2);

        let count = Vector::length(&v2);
        Utils::print(count);
    }
}

输出结果:

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

结构体不可递归

address 0x1{
    module person {
        struct Person has drop, copy {
            id: u64,
            age: u8,
            sex: bool   //without comma
        }

        public fun new_person(_id:u64, _age:u8, _sex:bool) : Person {
            return Person {
                id: _id,
                age: _age,
                sex: _sex,   //with comma
            }
        }

        public fun getId(p: Person) : u64 {
            return p.id
        }

        public fun getAge(p: Person) : u8 {
            return p.age
        }

        public fun getSex(p: Person) : bool {
            return p.sex
        }
    }
}

script {
    use 0x1::Debug;
    use 0x1::person;

    fun test_struct(){
        let p = person::new_person(110111, 20, true);
        let id = person::getId(p);
        let age = person::getAge(p);
        let sex = person::getSex(p);

        Debug::print(&id);
        Debug::print(&age);
        Debug::print(&sex);
    }
}

输出结果:

debug: 110111

debug: 20

debug: true

Gas used: 21

泛型结构

Move所有权

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

  • move 属主转移
  • copy 拷贝值
module 0x1::Utils{ 
    use 0x1::Debug; 
    //Generic 
    public fun print<T:drop>(a: T) { 
        Debug::print(&a); 
    } 
}

script {
    use 0x1::Utils;

    fun test_ownership() {
        let a = 5;
        Utils::print(copy a);
        Utils::print(move a);
        //Utils::print(a);
    }
}

Signer(发送交易者)

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

struct Signer has drop {

        addr: address

}

Signer API

  • address_of(&Signer):address //返回Signer地址
  • borrow_address(&Signer) : &address //返回Signer地址的引用
script {
    use 0x1::Signer;
    use 0x1::Debug;

    fun test_signer(sig: signer) {
        let addr = Signer::address_of(&sig);
        Debug::print(&addr);

        let addr_ref = Signer::borrow_address(&sig);
        Debug::print(addr_ref);
    }
}

运行输入:

test_signer(0x55)

输出结果: 

debug: 0000000000000000000000000000000000000000000000000000000000000055

debug: 0000000000000000000000000000000000000000000000000000000000000055

Gas used: 13

Resource(资源)

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

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

Resource API 

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

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

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

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

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

module 0x1::collection {
    use 0x1::Vector;
    use 0x1::Signer;

    struct Item has store, drop {}

    struct Collection has store, key {
        items: vector<Item>,
    }

    public fun create_resource(sig: &signer)  {
        move_to<Collection>(sig, Collection{
            items: Vector::empty<Item>()
        })
    }

    public fun is_exist_resource(addr: address) : bool {
        exists<Collection>(addr)
    }

    public fun add_resource(sig : &signer) acquires Collection {
        let addr = Signer::address_of(sig);
        let collection = borrow_global_mut<Collection>(addr);
        Vector::push_back(&mut collection.items, Item{});
    }

    public fun size_resource(sig: &signer): u64 acquires Collection {
        let addr = Signer::address_of(sig);
        let collection = borrow_global<Collection>(addr);
        Vector::length(&collection.items)
    }

    public fun destory_resource(sig: &signer) acquires Collection {
        let addr = Signer::address_of(sig);
        let collectio = move_from<Collection>(addr);
        let Collection{items: _} = collectio;
    } 
}

script { 
    use 0x1::Debug;
    use 0x1::Signer;
    use 0x1::collection as col;

    fun test_resource(sig: signer) {

        let addr = Signer::address_of(&sig);

        let isExist = col::is_exist_resource(addr);

        Debug::print(&isExist);

        if (isExist) {
            col::destory_resource(&sig);
        };

        col::create_resource(&sig);

        col::add_resource(&sig);

        let size = col::size_resource(&sig);

        Debug::print(&size);

    }
}

猜你喜欢

转载自blog.csdn.net/ling1998/article/details/127784208