Move Playground
只包括 无符号整型、布尔型、地址 3种类型,没有像其它语言中有字符串类型。
| 数据类型 | 数据类型表示符号 |
|---|---|
| 无符号整型 | u8, u64, u128 |
| 布尔类型 | bool |
| 地址类型 | address, ex: Std, 0x1, Sender |
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 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
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
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
copy 可被复制(基本类型自带此能力)
drop 可被丢弃(基本类型自带此能力)
key 可作为键值
store 可存储到全局状态(基本类型自带此能力)
定义:
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 属主转移
- 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是原生数据类型,只有 一种能力drop,即类似于如下结构:
struct Signer has drop {
addr: address
}
- 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
资源是被限制了能力的结构,只有key与store
资源必须存储在账户下,一个账户同一时刻只能容纳一个资源
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);}
}