当前位置:网站首页>Rust learning: 6.2_ Tuples of composite types
Rust learning: 6.2_ Tuples of composite types
2022-08-10 07:43:00 【I don't want to be bald】
Rust学习:6.2_A tuple of composite types
前言
为了学习Rust,阅读了github上的Rust By Practice电子书,本文章只是用来记录自己的学习过程,感兴趣的可以阅读原书,希望大家都能掌握Rust!
元组
Tuples are formed by combining multiple types together,So it is a composite type,元组的长度是固定的,The order of the elements in the tuple is also fixed.
A tuple can be created with the following syntax:
fn main() {
let tup: (i32, f64, u8) = (500, 6.4, 1);
}
变量 tup is bound to a tuple of values (500, 6.4, 1),The type of the tuple is (i32, f64, u8),看到没?Tuples use parentheses to group multiple types together,简单吧?
You can use pattern matching or . operator to get the value in the tuple.
1.Deconstruct tuples with pattern matching
fn main() {
let tup = (500, 6.4, 1);
let (x, y, z) = tup;
println!("The value of y is: {}", y);
}
The above code first creates a tuple,然后将其绑定到 tup 上,接着使用 let (x, y, z) = tup; to complete a pattern match,因为元组是 (n1, n2, n3) 形式的,So we use exactly the same (x, y, z) form to match,The corresponding value in the tuple is bound to the variable x, y, z上.这就是解构:Use the same form to match values in a complex object.
2.用 . to access the tuple
Pattern matching allows us to retrieve all or part of the values in a tuple at once,If you only want to access a specific element,That pattern matching is a little more complicated,对此,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;
}
and arrays in other languages、字符串一样,元组的索引从 0 开始.
3.Example usage of tuples
Tuples are often used in function-returning scenarios,例如下面的代码,可以使用元组返回多个值:
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 字符串的所有权,然后计算字符串的长度,Then return the string ownership and string length to s2 和 len 变量.
在其他语言中,A structure can be used to declare a point in three-dimensional space,例如 Point(10, 20, 30),虽然使用 Rust Tuples can do that too:(10, 20, 30),But this writing has a very significant flaw:
does not have any clear meaning,在下一章节中,A tuple-like structure is mentioned,元组结构体,可以解决这个问题.
4.练习
1.
元组中的元素可以是不同的类型.The type signature of a tuple is (T1, T2, ...), 这里 T1, T2 is the type of the corresponding tuple member.
fn main() {
let _t0: (u8,i16) = (0, -1);
// A member of a tuple can also be a tuple
let _t1: (u8, (i16, u32)) = (0, (-1, 1));
// Fill in the blanks to make the code work
let t: (u8, __, i64, __, __) = (1u8, 2u16, 3i64, "hello", String::from(", world"));
}
修改:
fn main() {
let _t0: (u8,i16) = (0, -1);
// A member of a tuple can also be a tuple
let _t1: (u8, (i16, u32)) = (0, (-1, 1));
// Fill in the blanks to make the code work
let t: (u8, u16, i64, &str, String) = (1u8, 2u16, 3i64, "hello", String::from(", world"));
}
2.
The members of a tuple can be obtained using an index
// Modify where appropriate,make the code work
fn main() {
let t = ("i", "am", "sunface");
assert_eq!(t.1, "sunface");
}
修改:
// Modify where appropriate,make the code work
fn main() {
let t = ("i", "am", "sunface");
assert_eq!(t.2, "sunface");
}
3.
Tuples that are too long cannot be printed
// 修复代码错误
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.
Use pattern matching to deconstruct tuples
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.
Tuples can be used for function arguments and return values
fn main() {
// 填空,A little calculation is required
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() {
// 填空,A little calculation is required
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)
}
边栏推荐
猜你喜欢

Relaxation class: the boss will martial arts, who also can not hold up against!The charm of six sigma training

PLSQL学习第四天

神经网络样本太少怎么办,神经网络训练样本太少

The probability distribution and its application

34. Talk about why you want to split the database?What methods are there?

COLMAP+OpenMVS realizes 3D reconstruction mesh model of objects

34. 谈谈为什么要拆分数据库?有哪些方法?

ATH10 sensor reads temperature and humidity

iwemeta元宇宙:一个娃娃卖9999元,泡泡玛特认为一点也不贵

Introduction to the delta method
随机推荐
CV-人脸识别-2018:ArcFace
.NET-8.我的思想笔记
mysql数据库月增长量问题
搭建 risc-v 编译环境
VS2013-调试汇编代码-生成asm文件-结构体内存布局-函数参数压栈-调用约定
如何设计神经网络结构,神经网络设计与实现
NPU架构与算力分析
DGIOT支持工业设备租赁以及远程管控
Rust学习:6.2_复合类型之元组
day16--The use of the packet capture tool Charles
phpstudy开机自启
34. Talk about why you want to split the database?What methods are there?
iwemeta元宇宙:一个娃娃卖9999元,泡泡玛特认为一点也不贵
[深入研究4G/5G/6G专题-56]: L3信令控制-5-无线承载管理
ABAP Data Types 和XSD Type 映射关系以及XSD Type属性
第十六天&charles的基本操作
IDLE development wordCount program (5)
foreach遍历删除元素问题总结
Add spark related dependencies and packaging plugins (sixth bullet)
ATH10 sensor reads temperature and humidity