Move 学习记录
创始人
2024-02-05 17:54:26

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 integerlet 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 typelet b1 = true; let b2: bool = false;let b3: bool;b3 = true;Debug::print(&b1);Debug::print(&b2);Debug::print(&b3);// address typelet 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(param: T,...)

module 0x1::Utils{ use 0x1::Debug; public fun print(a: T) { Debug::print(&a); } 
}script {use 0x1::Utils;fun test_generic() { let a = 10u8; Utils::print(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();Vector::push_back(&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(&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;  //inmutableUtils::print(*c);let d = Vector::borrow_mut(&mut v, 0);*d = 90;Utils::print(*d);let v2 = Vector::empty();Vector::append(&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(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(&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

 

module 0x1::collection {use 0x1::Vector;use 0x1::Signer;struct Item has store, drop {}struct Collection has store, key {items: vector,}public fun create_resource(sig: &signer)  {move_to(sig, Collection{items: Vector::empty()})}public fun is_exist_resource(addr: address) : bool {exists(addr)}public fun add_resource(sig : &signer) acquires Collection {let addr = Signer::address_of(sig);let collection = borrow_global_mut(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(addr);Vector::length(&collection.items)}public fun destory_resource(sig: &signer) acquires Collection {let addr = Signer::address_of(sig);let collectio = move_from(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);}
}

相关内容

热门资讯

埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...