一:Fork the sui project
- 点击 <math xmlns="http://www.w3.org/1998/Math/MathML"> G i t H u b \mathit{GitHub} </math>GitHub 进入 <math xmlns="http://www.w3.org/1998/Math/MathML"> S u i \mathit {Sui} </math>Sui 存储库。
- 找到 <math xmlns="http://www.w3.org/1998/Math/MathML"> F o r k \mathit {Fork} </math>Fork,将该库分叉存储到你自己的账号下。
- 来到你自己的分叉,找到绿色的 <math xmlns="http://www.w3.org/1998/Math/MathML"> C o d e \mathit {Code} </math>Code 按钮,复制其中的 <math xmlns="http://www.w3.org/1998/Math/MathML"> H T T P S \mathit {HTTPS} </math>HTTPS 地址。
- 切换到你的终端命令行,
cd
进入你想要存放代码的位置,通过git clone <https url>
的方式,将其拉取到本地;或者直接在 <math xmlns="http://www.w3.org/1998/Math/MathML"> V s C o d e \mathit {VsCode} </math>VsCode 的欢迎页,选择克隆 <math xmlns="http://www.w3.org/1998/Math/MathML"> G i t \mathit {Git} </math>Git 仓库...,在绑定账号后即可一键拉取。 - 如果你是通过
git clone
的方式,那么在拉取结束后,通过cd sui
即可进入对应文件夹,也可以通过 <math xmlns="http://www.w3.org/1998/Math/MathML"> V s C o d e \mathit {VsCode} </math>VsCode 打开对应的文件夹进行开发;如果你是直接通过 <math xmlns="http://www.w3.org/1998/Math/MathML"> V s C o d e \mathit {VsCode} </math>VsCode 克隆的库,那么此时就已经进入了对应的位置,可以进行后续操作。
二:永远的 Hello World
不管你是用上述哪种方式将代码拉取到本地,现在你都已经在 <math xmlns="http://www.w3.org/1998/Math/MathML"> V s C o d e \mathit {VsCode} </math>VsCode 中来到了对应的文件夹,我们在这里来创建属于我们的第一个程序:sui move new my_first_package
,这行命令将创建一个名为 <math xmlns="http://www.w3.org/1998/Math/MathML"> m y _ f i r s t _ p a c k a g e \mathit {my\_first\_package} </math>my_first_package 的空包。
进入新创建的文件夹,在其中可以发现一个名为 <math xmlns="http://www.w3.org/1998/Math/MathML"> s o u r c e s \mathit {sources} </math>sources 的文件夹,和一个名为 <math xmlns="http://www.w3.org/1998/Math/MathML"> M o v e . t o m l \mathit {Move.toml} </math>Move.toml 的代码文件,后者是一个清单文件,主要用来定义一些依赖项、地址名的映射等功能,等以后实际用到了再详细进行解释,现在,我们只需要将目光放到前者,所编写的代码我们都需要放到这里面(除一些特殊的,比如测试代码以外)。
通过 touch my_first_package/sources/my_module.move
可以创建我们的第一个名为的 <math xmlns="http://www.w3.org/1998/Math/MathML"> m y _ m o d u l e . m o v e \mathit {my\_module.move} </math>my_module.move 的代码文件,将其打开。
如果打开后 <math xmlns="http://www.w3.org/1998/Math/MathML"> V s C o d e \mathit {VsCode} </math>VsCode 右下角弹出了报错,这是在提醒我们 <math xmlns="http://www.w3.org/1998/Math/MathML"> M o v e . t o m l \mathit {Move.toml} </math>Move.toml 文件当中的 <math xmlns="http://www.w3.org/1998/Math/MathML"> a d d r e s s \mathit {address} </math>address 设置错误,同时没有在其中找到 <math xmlns="http://www.w3.org/1998/Math/MathML"> v e r s i o n \mathit {version} </math>version 配置。
就本篇文章而言,你可以直接忽略这一问题,而如果你想解决的话也很简单,打开该文件,在 [ <math xmlns="http://www.w3.org/1998/Math/MathML"> p a c k a g e \mathit {package} </math>package] 下添加 <math xmlns="http://www.w3.org/1998/Math/MathML"> v e r s i o n \mathit {version} </math>version = " <math xmlns="http://www.w3.org/1998/Math/MathML"> 1.0.0 \text {1.0.0} </math>1.0.0",在 [ <math xmlns="http://www.w3.org/1998/Math/MathML"> a d d r e s s \mathit {address} </math>address] 下的 <math xmlns="http://www.w3.org/1998/Math/MathML"> 0 x \text {0} \mathit {x} </math>0x 后随便添加几个数字,比如 <math xmlns="http://www.w3.org/1998/Math/MathML"> 123 \text {123} </math>123。
当然,在实际开发过程中这么做可能会略显草率,但还是那句话,对于本篇章内容而言无伤大雅,更详细的内容会在后续用到的时候详解。
move
module my_first_package::my_module {
use std::debug;
use std::string;
public fun hello_world() {
debug::print(&string::utf8(b"Hello World"));
}
#[test]
fun hello_world_text() {
hello_world();
}
}
module <address>::<module_name>
是每一份代码文件的开始,由于在 <math xmlns="http://www.w3.org/1998/Math/MathML"> M o v e . t o m l \mathit {Move.toml} </math>Move.toml 当中赋予了 <math xmlns="http://www.w3.org/1998/Math/MathML"> m y _ f i r s t _ p a c k a g e \mathit {my\_first\_package} </math>my_first_package 一个地址,所以这里可以直接用其替代以提高可读性, <math xmlns="http://www.w3.org/1998/Math/MathML"> m o d u l e _ n a m e \mathit {module\_name} </math>module_name 一般与文件同名,这样有利于其它文件导入该份代码后的调用。
第二、三行是通过 <math xmlns="http://www.w3.org/1998/Math/MathML"> u s e \mathit {use} </math>use 关键词将标准库中的部分内容( <math xmlns="http://www.w3.org/1998/Math/MathML"> d e b u g 、 s t r i n g \mathit {debug、string} </math>debug、string)导入到该份代码,如果想要调用自己写的其它代码,也是类似的格式:use <address>::module_name
,如果还想要更精细化到具体函数,那么只需要在后面再跟两个冒号以及具体的函数名即可。
当然,可能会存在导入模块同名的情况,不用担心, <math xmlns="http://www.w3.org/1998/Math/MathML"> m o v e \mathit {move} </math>move 提供了 <math xmlns="http://www.w3.org/1998/Math/MathML"> a s \mathit {as} </math>as 关键词,例如 use std::string as S
,就可以用 <math xmlns="http://www.w3.org/1998/Math/MathML"> S \mathit S </math>S 来代替 <math xmlns="http://www.w3.org/1998/Math/MathML"> s t r i n g \mathit {string} </math>string,这样就解决了可能会同名的问题。
第五、六、七行是一个公共的(谁都可以调用的)的函数,这个函数很简单,调用 <math xmlns="http://www.w3.org/1998/Math/MathML"> d e b u g \mathit {debug} </math>debug 模块下的 <math xmlns="http://www.w3.org/1998/Math/MathML"> p r i n t \mathit {print} </math>print 函数,将 <math xmlns="http://www.w3.org/1998/Math/MathML"> H e l l o W o r l d \mathit {Hello World} </math>HelloWorld 通过 <math xmlns="http://www.w3.org/1998/Math/MathML"> s t r i n g \mathit {string} </math>string 模块下的 <math xmlns="http://www.w3.org/1998/Math/MathML"> u t f 8 \mathit {utf} \text {8} </math>utf8 函数转化成字符串后打印输出,这里需要加上引用&
,因为 <math xmlns="http://www.w3.org/1998/Math/MathML"> p r i n t \mathit {print} </math>print 函数接收的参数就是引用类型的。
至此,如果将程序发布上链再进行调用,是可以得到 <math xmlns="http://www.w3.org/1998/Math/MathML"> d e b u g \mathit {debug} </math>debug 的打印输出的,但是如果想要在本地环境测试的话呢?那就要用到第九行的#[test]
。
这一行定义表明了接下去编撰的 <math xmlns="http://www.w3.org/1998/Math/MathML"> h e l l o _ w o r l d _ t e x t \mathit {hello\_world\_text} </math>hello_world_text 函数只在测试时执行,而这个测试函数的内容也很简单,就是单纯地调用 <math xmlns="http://www.w3.org/1998/Math/MathML"> h e l l o _ w o r l d \mathit {hello\_world} </math>hello_world。
sui move build
进行构建,再通过sui move test
进行测试,可以得到如下结果。
其中,[ <math xmlns="http://www.w3.org/1998/Math/MathML"> d e b u g \mathit {debug} </math>debug] 后的内容就是我们输出的 <math xmlns="http://www.w3.org/1998/Math/MathML"> H e l l o W o r l d \mathit {Hello World} </math>HelloWorld。
三:永远的 a + b
在通过测试的情形下完成的 <math xmlns="http://www.w3.org/1998/Math/MathML"> H e l l o W o r l d \mathit {Hello World} </math>HelloWorld,是不是跟大多数人心中的预期不符?尤其是跟一些已经学过的开发语言相比,这段代码显得非常臃肿?而且,虽然用 #[ <math xmlns="http://www.w3.org/1998/Math/MathML"> t e s t \mathit {test} </math>test] 的方式让测试函数只在测试时运行,可一旦代码量大起来,测试逻辑复杂,通篇不就更加冗杂了?
因此,我们用另一个例子,也就是 <math xmlns="http://www.w3.org/1998/Math/MathML"> a + b \mathit a + \mathit b </math>a+b 来演示如何将测试用代码分离开,还大家一个干净整洁的编码环境。
根据之前的学习,我们已经知道了, <math xmlns="http://www.w3.org/1998/Math/MathML"> s o u r c e s \mathit {sources} </math>sources 文件夹下主要是放我们自己开发的功能模块的,那么测试代码呢?同理,只需要在与 <math xmlns="http://www.w3.org/1998/Math/MathML"> s o u r c e s \mathit {sources} </math>sources 同级别目录下创建一个 <math xmlns="http://www.w3.org/1998/Math/MathML"> t e s t s \mathit {tests} </math>tests 文件夹即可。
对应一个模块的测试代码,一般用 <math xmlns="http://www.w3.org/1998/Math/MathML"> m o d u l e _ n a m e _ t e s t s \mathit {module\_name\_tests} </math>module_name_tests 来命名。
我们来更改 <math xmlns="http://www.w3.org/1998/Math/MathML"> m y _ m o d u l e . m o v e \mathit {my\_module.move} </math>my_module.move,删除其中的原有代码,编写一个函数来执行 <math xmlns="http://www.w3.org/1998/Math/MathML"> a + b \mathit a + \mathit b </math>a+b:
move
module my_first_package::my_module {
public fun add(a: u64, b: u64): u64 {
a + b
}
}
a: u64
表示这个函数的第一个参数,需要传入一个u64
类型的值,以a
来命名。
函数后的: u64
表示这个函数返回一个类型为u64
的值。
a + b
没有加分号,这不是错误,因为在 <math xmlns="http://www.w3.org/1998/Math/MathML"> m o v e \mathit {move} </math>move 语言当中,一个有返回值的块当中的最后一句不加分号的语句的结果,就相当于是其返回值,效果等同于return a + b;
注意此时就必须加分号了。
接着我们把目光放到测试文件上,虽然将其独立了出来,但是还是需要用 #[ <math xmlns="http://www.w3.org/1998/Math/MathML"> t e s t _ o n l y \mathit {test\_only} </math>test_only] 来注明这是测试时才载入的模块,用 #[ <math xmlns="http://www.w3.org/1998/Math/MathML"> t e s t \mathit {test} </math>test] 来注明这是测试时自动调用的函数。
测试模块开头的定义规则是类似的,都是module <address>::module_name
,最后的模块名一般都是对应测试的功能模块的名称后加 <math xmlns="http://www.w3.org/1998/Math/MathML"> _ t e s t s \mathit {\_tests} </math>_tests。
在这里,我们需要调用 <math xmlns="http://www.w3.org/1998/Math/MathML"> m y _ m o d u l e \mathit {my\_module} </math>my_module 模块当中的函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> a d d \mathit {add} </math>add,因此我们需要先将其导入,导入规则在之前有过解释,与导入标准库等其它功能模块类似。
在test_add
函数当中,我们用到了assert
,它需要传入两个参数,第一个参数是一个布尔类型的值,第二个参数是错误代码。
在这里,第一个参数用来传入我们通过add
得到的结果与期望结果之间的相等判断,第二个参数暂时用 <math xmlns="http://www.w3.org/1998/Math/MathML"> 0 \text 0 </math>0 来传入,实际开发过程中请以具体情况来。
move
#[test_only]
module my_first_package::my_module_tests {
use my_first_package::my_module;
#[test]
fun test_add() {
assert!(my_module::add(1, 2) == 3, 0);
}
}
此时,我们再来sui move test
,我们可以得到一个充满生机的绿色 <math xmlns="http://www.w3.org/1998/Math/MathML"> P A S S \mathit {PASS} </math>PASS。
如果想要尝试测试错误,只需要将上述测试代码上的期望结果另外填一个数即可,这个时候运行测试,会得到对应的报错信息。在实际开发过程中,就可以通过报错信息进行定位,快速找到问题所在。
四:加入组织,共同进步!
- Sui 中文开发群(TG)
- <math xmlns="http://www.w3.org/1998/Math/MathML"> M o v e \mathit{Move} </math>Move 语言学习交流群: 79489587