[初学rust] 06_rust 元组

rust 元组

表现形式

和python的元组类似,rust中的元组是一个有序列表,可以包含多种不同类型的数据。

rust 复制代码
let tup = (500, 6.4, 'a');

模式匹配解构元组

和python中的解构一样,rust也支持模式匹配解构元组,但是需要注意的是,如果元组中有多个相同类型的变量,那么必须使用_来跳过。

rust 复制代码
let tup = (500, 6.4, 'a');
let (x, y, _) = tup;
println!("The value of x is: {}", x);
println!("The value of y is: {}", y);

访问元组

这个就和C++的tuple类似,通过点号来访问。

rust 复制代码
let tup = (500, 6.4, 'a');
println!("The value of the tuple is: {}", tup);
println!("The value of the first element in the tuple is: {}", tup.0);
println!("The value of the second element in the tuple is: {}", tup.1);
相关推荐
雅俗共赏zyyyyyy2 分钟前
SpringBoot集成配置文件加解密
java·spring boot·后端
计算机学姐9 分钟前
基于SpringBoot的送货上门系统【2026最新】
java·vue.js·spring boot·后端·mysql·spring·tomcat
码农水水13 分钟前
国家电网Java面试被问:二叉树的前序、中序、后序遍历
java·开发语言·面试
Respect@17 分钟前
qml之TableViewColumn
开发语言·qml
不吃橘子的橘猫23 分钟前
NVIDIA DLI 《Build a Deep Research Agent》学习笔记
开发语言·数据库·笔记·python·学习·算法·ai
算法与双吉汉堡27 分钟前
【短链接项目笔记】6 短链接跳转
java·开发语言·笔记·后端·springboot
飞浪28 分钟前
告别“Hello World”:一个有经验的程序员如何用 FastAPI 打造生产级后端模板
后端
学Linux的语莫28 分钟前
python的基础使用
开发语言·python
独自破碎E32 分钟前
IDEA2023中新建Spring Boot2.X版本的工程的方法
java·spring boot·后端
wildlily842743 分钟前
C++ Primer 第5版章节题 第十章
开发语言·c++