当前位置:网站首页>Rust学习:6.2_复合类型之元组
Rust学习:6.2_复合类型之元组
2022-08-10 07:33:00 【我不想头秃阿】
Rust学习:6.2_复合类型之元组
前言
为了学习Rust,阅读了github上的Rust By Practice电子书,本文章只是用来记录自己的学习过程,感兴趣的可以阅读原书,希望大家都能掌握Rust!
元组
元组是由多种类型组合到一起形成的,因此它是复合类型,元组的长度是固定的,元组中元素的顺序也是固定的。
可以通过以下语法创建一个元组:
fn main() {
let tup: (i32, f64, u8) = (500, 6.4, 1);
}
变量 tup
被绑定了一个元组值 (500, 6.4, 1)
,该元组的类型是 (i32, f64, u8)
,看到没?元组是用括号将多个类型组合到一起,简单吧?
可以使用模式匹配或者 .
操作符来获取元组中的值。
1.用模式匹配解构元组
fn main() {
let tup = (500, 6.4, 1);
let (x, y, z) = tup;
println!("The value of y is: {}", y);
}
上述代码首先创建一个元组,然后将其绑定到 tup
上,接着使用 let (x, y, z) = tup;
来完成一次模式匹配,因为元组是 (n1, n2, n3)
形式的,因此我们用一模一样的 (x, y, z)
形式来进行匹配,元组中对应的值会绑定到变量 x
, y
, z
上。这就是解构:用同样的形式把一个复杂对象中的值匹配出来。
2.用 .
来访问元组
模式匹配可以让我们一次性把元组中的值全部或者部分获取出来,如果只想要访问某个特定元素,那模式匹配就略显繁琐,对此,Rust 提供了 .
的访问方式:
fn main() {
let x: (i32, f64, u8) = (500, 6.4, 1);
let five_hundred = x.0;
let six_point_four = x.1;
let one = x.2;
}
和其它语言的数组、字符串一样,元组的索引从 0 开始。
3.元组的使用示例
元组在函数返回值场景很常用,例如下面的代码,可以使用元组返回多个值:
fn main() {
let s1 = String::from("hello");
let (s2, len) = calculate_length(s1);
println!("The length of '{}' is {}.", s2, len);
}
fn calculate_length(s: String) -> (String, usize) {
let length = s.len(); // len() 返回字符串的长度
(s, length)
}
calculate_length
函数接收 s1
字符串的所有权,然后计算字符串的长度,接着把字符串所有权和字符串长度再返回给 s2
和 len
变量。
在其他语言中,可以用结构体来声明一个三维空间中的点,例如 Point(10, 20, 30)
,虽然使用 Rust 元组也可以做到:(10, 20, 30)
,但是这样写有个非常重大的缺陷:
不具备任何清晰的含义,在下一章节中,会提到一种与元组类似的结构体,元组结构体
,可以解决这个问题。
4.练习
1.
元组中的元素可以是不同的类型。元组的类型签名是 (T1, T2, ...)
, 这里 T1
, T2
是相对应的元组成员的类型.
fn main() {
let _t0: (u8,i16) = (0, -1);
// 元组的成员还可以是一个元组
let _t1: (u8, (i16, u32)) = (0, (-1, 1));
// 填空让代码工作
let t: (u8, __, i64, __, __) = (1u8, 2u16, 3i64, "hello", String::from(", world"));
}
修改:
fn main() {
let _t0: (u8,i16) = (0, -1);
// 元组的成员还可以是一个元组
let _t1: (u8, (i16, u32)) = (0, (-1, 1));
// 填空让代码工作
let t: (u8, u16, i64, &str, String) = (1u8, 2u16, 3i64, "hello", String::from(", world"));
}
2.
可以使用索引来获取元组的成员
// 修改合适的地方,让代码工作
fn main() {
let t = ("i", "am", "sunface");
assert_eq!(t.1, "sunface");
}
修改:
// 修改合适的地方,让代码工作
fn main() {
let t = ("i", "am", "sunface");
assert_eq!(t.2, "sunface");
}
3.
过长的元组无法被打印输出
// 修复代码错误
fn main() {
let too_long_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
println!("too long tuple: {:?}", too_long_tuple);
}
修改:
fn main() {
let too_long_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
println!("too long tuple: {:?}", too_long_tuple);
}
4.
使用模式匹配来解构元组
fn main() {
let tup = (1, 6.4, "hello");
// 填空
let __ = tup;
assert_eq!(x, 1);
assert_eq!(y, "hello");
assert_eq!(z, 6.4);
}
修改:
fn main() {
let tup = (1, 6.4, "hello");
// 填空
let (x, z, y) = tup;
assert_eq!(x, 1);
assert_eq!(y, "hello");
assert_eq!(z, 6.4);
}
5.
解构式赋值
fn main() {
let (x, y, z);
// 填空
__ = (1, 2, 3);
assert_eq!(x, 3);
assert_eq!(y, 1);
assert_eq!(z, 2);
}
修改:
fn main() {
let (x, y, z);
// 填空
(y, z, x) = (1, 2, 3);
assert_eq!(x, 3);
assert_eq!(y, 1);
assert_eq!(z, 2);
}
6.
元组可以用于函数的参数和返回值
fn main() {
// 填空,需要稍微计算下
let (x, y) = sum_multiply(__);
assert_eq!(x, 5);
assert_eq!(y, 6);
}
fn sum_multiply(nums: (i32, i32)) -> (i32, i32) {
(nums.0 + nums.1, nums.0 * nums.1)
}
修改:
fn main() {
// 填空,需要稍微计算下
let (x, y) = sum_multiply((2, 3));
assert_eq!(x, 5);
assert_eq!(y, 6);
}
fn sum_multiply(nums: (i32, i32)) -> (i32, i32) {
(nums.0 + nums.1, nums.0 * nums.1)
}
边栏推荐
- 个人博客系统
- 每日一题,二叉树中增加一行
- Confluence可以连接数据库但是在下一步就报错了
- 软件测试面试题避雷(HR面试题)最常见的面试问题和技巧性答复
- pytest之parametrize参数化
- If the data of the oracle business table is added, deleted, or modified, will the index of the table write redo and undo?
- Using the color picker
- 时序动作定位 | ACGNet:弱监督时序动作定位的动作补充图网络(AAAI 2022)
- 【MySQL】使用MySQL Workbench软件新建表
- 时序动作定位 | ASM-Loc:弱监督时序动作定位的动作感知片段建模(CVPR 2022)
猜你喜欢
初使jest 单元测试
每日一题,数组字符串的匹配问题
Chapter 11 Database Design Specifications [2. Index and Tuning] [MySQL Advanced]
MySQL设置初始密码—注意版本mysql-8.0.30
时序动作定位 | ASM-Loc:弱监督时序动作定位的动作感知片段建模(CVPR 2022)
What is an MQTT gateway?What is the difference with traditional DTU?
什么是长轮询
BUUCTF Notes (web)
The precise effect of network integration promotion outsourcing in the era of Internet of Things-Shenzhen Win-Win World News
mysql之两阶段提交
随机推荐
The probability distribution and its application
DGIOT支持工业设备租赁以及远程管控
自动化测试框架Pytest(三)——自定义allure测试报告
oracle业务表的数据发生增删改,该表的索引会写redo,undo吗?
QT下载清华源配置
Regular backup of mysql database (retain backups for nearly 7 days)
navicat for mysql 连接时报错:1251-Client does not support authentication protocol requested by server
ATH10 sensor reads temperature and humidity
神经网络可视化有3D版本了,美到沦陷 已开源
金融证券 初级 招股书 要求 黑话1刷数 黑话2底稿 黑话3董监高
Discussion on Chinese Fuzzy Retrieval in Databases
神经网络的三种训练方法,神经网络训练全过程
mysql数据库定时备份(保留近7天的备份)
全连接神经网络结构图,神经网络示意图怎么画
Synchronization lock synchronized traces the source
Deep understanding of the array
If the data of the oracle business table is added, deleted, or modified, will the index of the table write redo and undo?
【Event Preview on August 9】Prometheus Summit
3.1-3.3 读书笔记
阿里巴巴(中国)网络技术有限公司、测试开发笔试二面试题(附答案)