Move 学习记录

news2024/10/5 19:00:56

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);

    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/23434.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

应急响应-文件痕迹排查

文件痕迹排查文件痕迹排查Windows文件痕迹排查敏感目录时间点文件排查Linux文件痕迹排查敏感目录时间点排查特殊文件文件痕迹排查 在应急响应排查的过程中&#xff0c;由于大部分的恶意软件、木马、后门等都会在文件维度上留下痕迹&#xff0c;因此对文件痕迹的排查必不可少。…

【数据挖掘】关联规则挖掘

关联规则挖掘 一、基本概念 概念 关联规则挖掘(Association rules mining) 在数据集中找到各项数据之间的关联关系。 项目(Item) IDItems bought10A,B,D20A,C,D30A,D,E 支持度(support) 项集{x,y}\{x,y\}{x,y}在总项集中出现的概率(x,yx,yx,y同时出现) Support(x→y)P(x,…

使用ESP32连接腾讯云实现远程控制方法

​大家好&#xff0c;上次给大家分享了如何使用ESP32实现蓝牙通信&#xff0c;今天跟大家聊聊如何使用ESP32连接腾讯云实现远程控制。本次实验用到MQTT协议&#xff0c;同样&#xff0c;我用miropython编写程序实现&#xff0c;最终可以通过腾讯连连微信小程序添加设备来发布主…

【GamePlay】两个ScrollView插件,Super ScrollView UIExtensions

前言 记录一下最近了解使用的两个滚动列表插件&#xff0c;UIExtensions 和Super ScrollView 。 原生的ScrollView 只能滚动 Viewport中Content范围内的物体&#xff0c;如果要使用必须得做一些功能扩展&#xff0c;很不方便&#xff0c;于是找了这两个插件。 前者不只是滚动…

一文了解JVM整体设计

一、JDK / JRE / JVM二、.Java 文件运行过程三、JVM运行时数据区3.1 Method Area3.2 Heap3.3 Java Virtual Machine Stacks3.4 Native Method Stacks3.5 The PC Register四、JVM内存模型4.1 JVM对象创建和回收过程五、垃圾收集5.1 确定垃圾收集对象5.1.1 引用计数法5.1.2 可达性…

2023年英语二大作文押题猜想(达立易考)

又到了考前大开脑洞的时间了&#xff01;每年一到这个时间点&#xff0c;关于押题猜题的话题就会铺天盖地而来&#xff0c;众多名师大咖更是会集毕生所学&#xff0c;期待可以在这个环节押中部分题目彰显实力&#xff0c;其中主观题就是大家集中关注的重要热点模块。押题听起来…

声纹识别开源工具 ASV-Subtools

今天非常荣幸有机会在Speechhome语音技术研讨会上分享我们团队在开源项目上的一些工作。今天我分享的主题是声纹识别开源工具ASV-Subtools。 今天我分享的主要有5个部分的内容&#xff0c;分别是背景介绍、工具介绍、实验结果、Subtools工程化、总结与展望。其中Subtools工程化…

【ROS】机械人开发五--ROS基本概念的程序实现

机械人开发五--ROS基本概念的程序实现一、开发工具二、RoboWare Studio的基本使用2.1 软件启动2.2 修改界面语言2.3 使用2.4 编译文件2.5 卸载三、话题通信四、话题的代码编写4.1 发布端4.2 接收端4.3 测试五、自定义消息5.1 自定义消息类型5.2 自定义消息发布端5.3 自定义消息…

el-menu动态加载路由,菜单的解决方案

先看需要实现的效果 这里有一级也有二级菜单&#xff0c;注意二级菜单的父目录&#xff08;”选项设置“点击不会跳转&#xff0c;只是展开目录&#xff09;&#xff0c;然后点击去详情页&#xff0c;需要跳到一个隐藏的路由&#xff0c;不在菜单展示的路由 还有一点要注意&…

LaTex常用技巧5:公式太长换行并加大括号

使用LaTex做笔记的时候发现公式太长&#xff0c;一行会超出页面&#xff0c;于是想到换行。 原来的代码&#xff0c;这里使用了包bm&#xff0c;测试的时候前面请使用\usepackage{bm}。 \begin{equation}_{i}^{G} {\bm{a}}\begin{cases} _{i}^{i-1}\ddot{\bm{p}}, &i1\\_…

web课程设计网页规划与设计 html+css+javascript+jquery+bootstarp响应式游戏网站Bootstrap模板(24页)

&#x1f389;精彩专栏推荐&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb; ✍️ 作者简介: 一个热爱把逻辑思维转变为代码的技术博主 &#x1f482; 作者主页: 【主页——&#x1f680;获取更多优质源码】 &#x1f393; web前端期末大作业…

网址,URL,域名,IP地址,DNS,域名解析(转载)

一、基本常识 互联网上的所有数据都是存储在主机(服务器)上互联网中的所有主机都拥有唯一的IP地址互联网中任意两台主机通信都是通过IP地址来实现 上网的实质 就是获取网址对应主机上的数据并在用户主机上进行展示(浏览器上) 疑问&#xff1a;互联网中的任意两台主机通信是依…

详细介绍百度ERNIE:通过知识集成增强表示

文章目录ERNIE简介相关工作采用矢量表示单词 &#xff0c;上下文无关的表示采用上下文来预测丢失的单词&#xff0c;没有考虑先验知识采用异构数据ERNIE的详细实现Transformer编码器知识整合基本级别掩码短语级别掩码实体级别掩码实验异构语料库预训练DLM&#xff08;对话语言模…

python自动化:桌面壁纸下载器,满足你对桌面壁纸的无限畅想!

随着计算机性能的提升&#xff0c;人们对计算机个性化的要求也越来越高了&#xff0c;自己使用的计算机当然要设置成自己喜欢的风格&#xff01; 对于桌面壁纸有着强烈要求的朋友们有福了&#xff0c;推荐一个微软免费畅玩无限高清壁纸的官网。 https://cn.bing.com/images/t…

深入学习数组

目录 一、一维数组 1、数组的创建和初始化 2、一维数组的使用 3、*一维数组在内存中的存储 二、二维数组 1、二维数组的创建和初始化 2、二维数组的使用 3、*二维数组在内存中的存储 三、数组越界 一、一维数组 1、数组的创建和初始化 数组是一组相同类型元素的集合。 数组…

UNIAPP实战项目笔记42 购物车页面新增收货地址

UNIAPP实战项目笔记42 购物车页面新增收货地址 设置新增收货地址页面布局和功能 具体内容图片自己替换哈&#xff0c;随便找了个图片的做示例 用到了vuex的状态机,具体位置见目录结构 代码 my-add-path.vue 页面部分 my-add-path.vue 设置页面布局 用到了vuex的状态机 <te…

steam搬砖副业,月入2万+,内含全套讲解

Steam平台是一款国外知名的数字游戏社交平台&#xff0c;steam游戏平台起初只是一个整合游戏的下载平台&#xff0c;随着软件的发展&#xff0c;逐渐演变为了游戏社交平台&#xff0c;steam是世界上目前最大的游戏平台之一&#xff0c;而「网易BUFF」是一款由网易公司推出&…

Unity3D简陋版跑酷游戏

目录 功能演示 功能简介 制作步骤 功能演示 链接&#xff1a;https://pan.baidu.com/s/1E_2JXWlVJNf3S5l-dH2UuQ提取码&#xff1a;dm5e 视频教学:Unity3D大作业 超级简陋版的跑酷游戏_哔哩哔哩_bilibili 功能简介 本次跑酷游戏主要从跑道&#xff0c;UI设计&#xff0c;…

[附源码]java毕业设计小区宠物管理系统

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

MyBatis--获取参数值

MyBatis获取参数值的两种方式 &#xff1a; ${} 和 #{} ${}的本质是字符串 &#xff0c;#{}的本质是占位符赋值 ${}使用字符串拼接的方式拼接sql &#xff0c;若为字符串类型或日期类型的字段进行赋值时&#xff0c;需要手动加单引号。 #{}使用占位符赋值的方式拼接sql &#x…