当前位置:网站首页>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
报错如下:
边栏推荐
- 阿里高工带来的20022最新面试总结太香了
- 位图与位运算
- Shell之常用小工具(sort、uniq、tr、cut)
- 内网穿透工具ngrok使用教程
- Glory to the Blue Yonder, speeds up the strategic growth
- Reading and writing after separation, performance were up 100%
- 用场景定义硬件,英码科技破解“边缘计算”密码
- [Microservice ~ Remote Call] Integrate RestTemplate, WebClient, Feign
- 罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...
- Golang学习之路(五):Golang的函数
猜你喜欢
Here comes the question: Can I successfully apply for 8G memory on a machine with 4GB physical memory?
国产抗新冠口服药每瓶不超300元/ 我国IPv6网络全面建成/ 谷歌入局折叠屏手机...今日更多新鲜事在此...
The batch size does not have to be a power of 2!The latest conclusions of senior ML scholars
【Untitled】
GPT-3组合DALL·E,60秒内搞定游戏设定和原型动画!网友看后:这游戏想玩
非科班AI小哥火了:他没有ML学位,却拿到DeepMind的offer
900页数学论文证明旋转的黑洞不会爆炸,丘成桐:30多年来广义相对论首次重大突破...
问题来了:4GB物理内存的机器上申请8G内存能成功吗?
[Microservice ~ Remote Call] Integrate RestTemplate, WebClient, Feign
阻塞、非阻塞、多路复用、同步、异步、BIO、NIO、AIO 一锅端
随机推荐
腾讯欲成育碧最大股东/ 米哈游招NLP内容生成研究员/ AI发现四千余物种濒临灭绝...今日更多新鲜事在此...
又有大厂员工连续加班倒下/ 百度搜狗取消快照/ 马斯克生父不为他骄傲...今日更多新鲜事在此...
Intranet penetration tool ngrok usage tutorial
系统提供的堆 VS 手动改写堆
GPT-3组合DALL·E,60秒内搞定游戏设定和原型动画!网友看后:这游戏想玩
2022 Niu Ke Duo School (6) M. Z-Game on grid
自定义VIEW实现应用内消息提醒上下轮播
ABAP 面试题:如何使用 ABAP 编程语言的 System CALL 接口,直接执行 ABAP 服务器所在操作系统的 shell 命令?
FFmpeg在win10上编译安装(配置libx264)
Go Affair, How to Become a Gopher and Find a Go Job in 7 Days, Part 1
MongoDB-查询中$all的用法介绍
Information system project managers must memorize the core test sites (63) The main process of project portfolio management & DIPP analysis
基于STM32+铂电阻设计的测温仪
ABP 6.0.0-rc.1的新特性
MySQL5.6到8.0的账号迁移
Programmer's Exclusive Romance - Use 3D Engine to Realize Fireworks in 5 Minutes
Adalvo收购其首个品牌产品Onsolis
Compensation transaction and idempotency guarantee based on CAP components
Flutter入门进阶之旅(四)文本输入Widget TextField
go基础之web获取参数