一、基本类型
Move 的基本数据类型包括: 整型 (u8, u64, u128)、布尔型 boolean 和地址 address。
Move 不支持字符串和浮点数。
整型
整型包括 u8、u64 和 u128,我们通过下面的例子来理解整型:
script {
fun main() {
// define empty variable, set value later
let a: u8;
a = 10;// define variable, set type let a: u64 = 10; // finally simple assignment let a = 10; // simple assignment with defined value type let a = 10u128; // in function calls or expressions you can use ints as constant values if (a < 10) {}; // or like this, with type if (a < 10u8) {}; // usually you don't need to specify type } }
运算符as
当需要比较值的大小或者当函数需要输入不同大小的整型参数时,你可以使用as运算符将一种整型转换成另外一种整型:
script {
fun main() {
let a: u8 = 10;
let b: u64 = 100;// we can only compare same size integers if (a == (b as u8)) abort 11; if ((a as u64) == b) abort 11; } }
布尔型
布尔类型就像编程语言那样,包含false和true两个值。
script {
fun main() {
// these are all the ways to do it
let b : bool; b = true;
let b : bool = true;
let b = true
let b = false; // here’s an example with false
} }
地址<

本文介绍了Move语言的基本数据类型,包括整型(u8,u64,u128)、布尔型和地址。同时,讲解了如何使用整型、布尔型以及地址进行表达式操作,如as运算符用于类型转换。此外,还讨论了表达式和作用域的概念,包括let关键字用于声明变量,以及块表达式的返回值。文章强调了变量的作用域和生命周期,指出变量仅在其定义的作用域内有效。
最低0.47元/天 解锁文章
例子&spm=1001.2101.3001.5002&articleId=128610409&d=1&t=3&u=3af6ea7a19a24d7fa837ce30252db147)
3350

被折叠的 条评论
为什么被折叠?



