当前位置:网站首页>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
报错如下:
边栏推荐
猜你喜欢
h264协议
【HCIP持续更新】IS-IS协议原理与配置
Nature:猪死亡1小时后,器官再次运转
链表噩梦之一?5000多字带你弄清它的来龙去脉
HAproxy:负载均衡
合并两个有序列表
Information system project managers must memorize the core test sites (63) The main process of project portfolio management & DIPP analysis
Two minutes recording can pass by second language!The volcano how to practice and become voice tone reproduction technology?
Resolved IndentationError: unindent does not match any oute r indentation Level
世界第4疯狂的科学家,在103岁生日那天去世了
随机推荐
无重复字符的最长子串
微服务架构的核心关键点
【无标题】
李开复花上千万投的缝纫机器人,团队出自大疆
问题来了:4GB物理内存的机器上申请8G内存能成功吗?
注释、关键字、标识符的区别你知道吗?
国产抗新冠口服药每瓶不超300元/ 我国IPv6网络全面建成/ 谷歌入局折叠屏手机...今日更多新鲜事在此...
合并两个有序列表
1-hour live broadcast recruitment order: industry big names share dry goods, and enterprise registration opens丨qubit·viewpoint
[Microservice ~ Remote Call] Integrate RestTemplate, WebClient, Feign
AI basketball referee, walking is special, ask harden care don't care
Glory to the Blue Yonder, speeds up the strategic growth
Too much volume... Tencent was asked on the side that the memory was full, what would happen?
Flutter入门进阶之旅(四)文本输入Widget TextField
GPT-3组合DALL·E,60秒内搞定游戏设定和原型动画!网友看后:这游戏想玩
位图与位运算
保存Simulink仿真模型为图片或者PDF的方法
The redis library cannot be imported
数字化转型之支撑保障单元
Shell之常用小工具(sort、uniq、tr、cut)