Let's Move Sui环形掌上战争 0基础开发入门到精通(3)

  • Summer.XJ
  • 更新于 2024-03-28 16:31
  • 阅读 203

Sui环形掌上战争0基础开发入门到精通(3)环境的搭建有过编程基础的人,肯定熟悉各种开发环境,比如:C++、Java、Python等。Sui的编程环境,略复杂一些,官方的资料是用cargo来编译安装。cargoinstall--locked--githttps://github.

<!--StartFragment-->

Sui环形掌上战争 0基础开发入门到精通(3)

环境的搭建

有过编程基础的人,肯定熟悉各种开发环境,比如:C++、Java、Python等。 Sui的编程环境,略复杂一些,官方的资料是用cargo来编译安装。

cargo install --locked --git https://github.com/MystenLabs/sui.git --branch devnet sui

上述描述对于初学者并不是恨友好,因为很多东西都是陌生的,比如:cargo、git等。 没有rust基础的小伙伴们更是云里雾里,不知道如何下手。

所以我们需要做一些前期功课。从几方面开始着手

  • 目前自己的开发环境是什么?win10?linux?还是MacBook?

个人倾向于macbook,因为macbook的开发环境比较简单,不容易产生各种编译问题。 最难攻克的还是windows环境,根据官方文档,windows环境需要安装rust和cargo等一些列操作。 很大一部分新手就卡在编译环境上了。

环境的测试

先看看版本

sui --version
sui 1.19.1-041c5f2-dirty

阅读 sui client 的一些文档掌握必要的sui client命令是必须的

查看当前地址列表
sui client addresses
切换网络
sui client switch --env [network alias]
建立工程名
sui move new 工程名

提前做好测试

由于我们是0基础新手,所以在开发过程中,我们最好提前做好测试。

在代码里准备好测试的代码
#[test]

这样方便我们调试和学习,看到一段代码就可以做一些测试工作。 比如我们用new命令新建一个工程

sui move new hellow

得到了一个hellow工程目录 hellow

里面只有一个Move.toml文件,内容如下

[package]
name = "hellow"

# edition = "2024.alpha" # To use the Move 2024 edition, currently in alpha
# license = ""           # e.g., "MIT", "GPL", "Apache 2.0"
# authors = ["..."]      # e.g., ["Joe Smith (joesmith@noemail.com)", "John Snow (johnsnow@noemail.com)"]

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" }

# For remote import, use the `{ git = "...", subdir = "...", rev = "..." }`.
# Revision can be a branch, a tag, and a commit hash.
# MyRemotePackage = { git = "https://some.remote/host.git", subdir = "remote/path", rev = "main" }

# For local dependencies use `local = path`. Path is relative to the package root
# Local = { local = "../path/to" }

# To resolve a version conflict and force a specific version for dependency
# override use `override = true`
# Override = { local = "../conflicting/version", override = true }

[addresses]
hellow = "0x0"

# Named addresses will be accessible in Move as `@name`. They're also exported:
# for example, `std = "0x1"` is exported by the Standard Library.
# alice = "0xA11CE"

[dev-dependencies]
# The dev-dependencies section allows overriding dependencies for `--test` and
# `--dev` modes. You can introduce test-only dependencies here.
# Local = { local = "../path/to/dev-build" }

[dev-addresses]
# The dev-addresses section allows overwriting named addresses for the `--test`
# and `--dev` modes.
# alice = "0xB0B"

还有一个空的 sources 目录,里面什么都没有。

我们在这个souces目录下新建一个hellow.move文件,内容如下

module hellow::hellow{
    use std::debug::{Self,print};
    use std::string::{Self,utf8,String};

    entry public fun main(){
        print(&utf8(b"start game ... "));
    }
}

然后执行编译命令

sui move build

如果环境配置正确的话是可以编译成功的,但我们此时看不见任何输出。

尝试测试

sui move test

Running Move unit tests
Test result: OK. Total tests: 0; passed: 0; failed: 0

也是一无所获的。

所以,我们要加入测试代码,在hellow.move里加入测试代码

module hellow::hellow{
    use std::debug::{Self,print};
    use std::string::{Self,utf8,String};

    entry public fun main(){
        print(&utf8(b"start game ... "));
    }

    #[test]
    fun nowtest(){
        main();
    }
}

上述的测试代码加好后,就能执行main函数来看效果了。 输入如下:

Running Move unit tests
[debug] "start game ... "
[ PASS    ] 0x0::hellow::nowtest
Test result: OK. Total tests: 1; passed: 1; failed: 0

后面可以边写边测边看效果。一步一步的学习和积累。

趁手的工具集

vs code 或 idea 都是不错的开发工具IDE

sui-move-analyzer 是一个分析工具,可以分析move代码,已插件的形式存在在vs code中。

今天就讲到这,说了那么多,小伙们还是得需要自己一步一步手动搭建一个开发环境,并编写第一个move代码。

这样才能印象深刻。

(未完待续)

telegram: https\://t.me/move_cn

QQ群: 79489587

<!--EndFragment-->

点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
Summer.XJ
Summer.XJ
0x0d54...9e80
学好MOVE 光宗耀祖