当前位置:网站首页>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
报错如下:
边栏推荐
- The latest interview summary in 20022 brought by Ali senior engineer is too fragrant
- ansible-cmdb友好展示ansible收集主机信息
- MySQL 原理与优化,Group By 优化 技巧
- 位图与位运算
- Apexsqlrecover cannot connect to database
- 1小时直播招募令:行业大咖干货分享,企业报名开启丨量子位·视点
- Batch大小不一定是2的n次幂!ML资深学者最新结论
- 阿里高工带来的20022最新面试总结太香了
- Byte Qiu Zhao confused me on both sides, and asked me under what circumstances would the SYN message be discarded?
- 二叉树的序列化和反序列化
猜你喜欢
基于STM32+铂电阻设计的测温仪
LeetCode #101. Symmetric Binary Tree
基于CAP组件实现补偿事务与幂等性保障
Win10 compiles the x264 library (there are also generated lib files)
Flutter入门进阶之旅(五)Image Widget
AI basketball referee, walking is special, ask harden care don't care
非科班AI小哥火了:他没有ML学位,却拿到DeepMind的offer
系统提供的堆 VS 手动改写堆
读写分离后,性能居然提升100%了呀
GPT-3组合DALL·E,60秒内搞定游戏设定和原型动画!网友看后:这游戏想玩
随机推荐
FFmpeg compiles and installs on win10 (configure libx264)
用 API Factory 产品生成 API 文档
又有大厂员工连续加班倒下/ 百度搜狗取消快照/ 马斯克生父不为他骄傲...今日更多新鲜事在此...
【微服务~远程调用】整合RestTemplate、WebClient、Feign
h264 protocol
大佬们,请教一下,我看官方文档中,sqlserver cdc只支持2012版之后的,对于sqlser
AQS同步组件-FutureTask解析和用例
GPT-3组合DALL·E,60秒内搞定游戏设定和原型动画!网友看后:这游戏想玩
Flutter入门进阶之旅(三)Text Widgets
redis库没法引入
微信小程序支付及退款整体流程
AI basketball referee, walking is special, ask harden care don't care
已解决IndentationError: unindent does not match any oute r indentation Level
Scala 高阶(七):集合内容汇总(上篇)
无重复字符的最长子串
Shell之常用小工具(sort、uniq、tr、cut)
Go Affair, How to Become a Gopher and Find a Go Job in 7 Days, Part 1
WeChat payment development process
Nature:猪死亡1小时后,器官再次运转
读写分离后,性能居然提升100%了呀