当前位置:网站首页>Rust from entry to proficient 04 - data types
Rust from entry to proficient 04 - data types
2022-08-09 13:17:00 【51CTO】
Rust 是 静态类型(statically typed)语言,That is, the types of all variables must be known at compile time.
在 Rust 中,Every value belongs to one 数据类型(data type),分为两大类:
①、标量(scalar):整型、浮点型、布尔类型、字符类型
②、复合(compound):元祖(tuple)、数组(array)、结构体(struct)
1、标量scalar
Each type has a separate value.
1.1 整型
Represents a number without a fractional part,分为有符号(以 i 开头)和无符号(以 u 开头)整型.
The default type for numeric types is i32.
长度 | 有符号 | 无符号 |
8-bit | | |
16-bit | | |
32-bit | | |
64-bit | | |
128-bit | | |
arch | | |
Every signed integer can store inclusive slaves -(2^{n - 1}) 到 2^{n - 1} - 1 numbers inside,这里 n is the length of the integer definition.所以 i8
can be stored from -2^7到 2^7 - 1 numbers inside,也就是从 -128 到 127.Unsigned variants can be stored from 0 到 2^{n - 1} 的数字,所以 u8
can be stored from 0 到 2^8 - 1 的数字,也就是从 0 到 255.
另外,isize
和 usize
类型依赖运行程序的计算机架构:64 位架构上它们是 64 位的, 32 位架构上它们是 32 位的.
1.1.1 All numeric literals,You can add underscores anywhere_
1.1.2 A literal can be suffixed with a type,Represents a concrete type of number
1.1.3 Call the function directly on the integer literal
1.1.4 整数溢出
Rust The handling of integer overflow is as follows: ①、默认情况下,在debug模式下编译器会自动插入整数溢出检查,一旦发生溢出,则会引发 panic; ②、在 release 模式下,不检查整数溢出,It is a way of automatically discarding the high bits.
1.1.5 如何选择
Usually the default type i32 即可,It's usually the fastest.
1.2 浮点
Rust There are two native ones 浮点数(floating-point numbers)类型,它们是带小数点的数字.是基于 IEEE 754-2008 Standard floating point type,分别是 f32
和 f64
,分别占 32 位和 64 位.默认类型是 f64
,因为在现代 CPU 中,它与 f32
速度几乎一样,不过精度更高.
1.3 布尔类型
布尔类型(bool)代表的是“是”和“否”的二值逻辑.它有两个值:
true和false
Generally used in logical expressions,可以执行“与”“或”“非”等运算.
1.4、字符类型
The character type is given by char 表示.It can describe any match unicode 标准的字符值.在代码中,Single character literals are enclosed in single quotes(different from strings):
1.4.1 4个字节字符
因为 char Types are designed to describe either one unicode 字符,So the memory space it occupies is not1个字节,而是 4 个字节.
That means it can be compared ASCII Indicates more.在 Rust 中,拼音字母(Accented letters),中文、日文、韩文等字符,emoji(绘文字)as well as zero-length whitespace characters are valid char
值.Unicode A scalar value contains from U+0000
到 U+D7FF
和 U+E000
到 U+10FFFF
在内的值.
1.4.2 1个字节字符-u8
let x : u8 = 1;
对于 ASCII Characters actually only need to occupy one byte of space,因此Rust Single-byte character literals are provided to represent them ASCII 字符.
注意:We can also pass a letter b 在字符或者字符串前面,代表这个字面量存储在 u8 类型数组中,这样占用空间比 char 型数组要小一些.
let x : u8 = 1; let y : u8 = b'A';
2、复合compound
复合类型(Compound types)可以将多个值组合成一个类型
2.1 元祖(tuple)
①、由圆括号()Contains a set of expressions;
②、长度固定,一旦声明,其长度不会增大或缩小.
③、rustDifferent types of data types can be stored in
2.1.2 实例
2.1.3 如果元祖只有一个元素,A comma should be added,Used to distinguish between parenthesized expressions and tuples
2.1.4 Access the tuple element
①、Pattern matching destructuring
②、数字索引
2.2 数组(array)
①、by square brackets[] Contains a set of expressions;
②、数组中每个元素的类型必须相同(元祖tuple可以不同);
③、长度固定,一旦声明,其长度不会增大或缩小.
2.2.1 实例
There are three ways to declare.
2.2.2 访问数组元素
①、通过下标访问
The initial subscript is 0
②、通过 get() 方法
注意返回值是 Option<T>
2.2.3 数组越界访问异常
If the declared array has4个,But access subscript greater than or equal to4,则会在运行时抛出异常(编译能过).
2.2.4 Avoid array out-of-bounds program crashes
If we are not sure if it is legal to read the index of the array,The above access through the index will cause an exception,导致程序奔溃.
为了避免这种情况,我们可以使用 get(index) method to get an element in an array,其返回值是 Option<T>
2.3 结构体(struct)
Structures are similar to primitives,Multiple types can be combined together,as a new type. Structures can be divided into three specific types:
2.3.1 具名结构体
①、Separate each element with a comma,The last comma can be omitted. ②、The type still follows the colon,But automatic type deduction cannot be used,必须显示指定.
Local variables are the same as structure variables,Repeated colon initialization can be omitted
2.3.2 元祖结构体tuple struct
这是前面介绍的 tuple 和 struct A mix of both types,tuple struct Structures have names,But the members don't have names.
Parentheses around the name,Types have separate names,Members do not have individual names.
访问方法
通过下标访问:
2.3.3 单元结构体
Cell structures do not take up any memory space.
3、枚举 enum
如果说 tuple、struct、tuple struct 在 Rust represents multiple types“与”关系,那么 enurn类型在 Rust represents multiple types“或”关系.
Rust 的 enurn The definition syntax for each element in struct The definition syntax is similar.Can be like an empty struct,Do not specify its type;也可以像 tuple struct 一样,Use parentheses to add unnamed members;Also works like a normal struct,Use curly braces to add named members.
4、特殊数据类型
4.1 Never 类型
Represents a data type for which no return value is possible.
①、in type theory,called the bottom type,The base type does not contain any value,But it can be combined into any other type;
②、Never Type with an exclamation mark“!" 表示;
③、Not yet stable,但是rustInternal use has already started.
5、常见错误
5.1 Type conversion must be passed as 关键字显式声明
报错如下:
增加 as The keyword display statement is sufficient.
5.2 Compound data types allow recursion,But direct nesting is not allowed
报错如下:
边栏推荐
- 用皮肤“听”音乐,网友戴上这款装备听音乐会:仿佛住在钢琴里
- 发明时代,「幂集创新」事关你我
- 1-hour live broadcast recruitment order: industry big names share dry goods, and enterprise registration opens丨qubit·viewpoint
- Adalvo acquires its first branded product, Onsolis
- WeChat Mini Program Payment and Refund Overall Process
- 虚拟机安装出现的问题汇总
- 在北极都可以穿短袖了,温度飙升至32.5℃
- 香港服务器如何进行加密?
- The grep command Shell regular expressions, the three musketeers
- The batch size does not have to be a power of 2!The latest conclusions of senior ML scholars
猜你喜欢
随机推荐
曼城推出可检测情绪的智能围巾,把球迷给整迷惑了
Compensation transaction and idempotency guarantee based on CAP components
我们真的需要DApp吗?App真的不能满足我们的幻想吗?
Scala Advanced (7): Collection Content Summary (Part 1)
两分钟录音就可秒变语言通!火山语音音色复刻技术如何修炼而成?
读写分离后,性能居然提升100%了呀
The batch size does not have to be a power of 2!The latest conclusions of senior ML scholars
K个结点的组内逆序调整
【Untitled】
MySQL5.6到8.0的账号迁移
MySQL principle and optimization of Group By optimization techniques
数据挖掘-05
注释、关键字、标识符的区别你知道吗?
全面了解什么是TPS、QPS以及两者的区别
使用RecyclerView实现三级折叠列表
Flutter入门进阶之旅(一)-初识Flutter
链表噩梦之一?5000多字带你弄清它的来龙去脉
罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...
阿里高工带来的20022最新面试总结太香了
自定义VIEW实现应用内消息提醒上下轮播