[初学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);
相关推荐
3095566669 小时前
股票接口重试怎么分类:正常、空数组与错误响应的幂等处理
java·大数据·开发语言·数据库·php·api接口·股票数据api接口
郝学胜-神的一滴11 小时前
Effective STL 条款09:慎重选择删除元素的方法
开发语言·网络·数据结构·c++·算法·stl
努力的小雨11 小时前
手里捏着纸,谁来暂停视频?我把折纸老师塞进了 AI 眼镜
后端
狗头大军之江苏分军11 小时前
《潮水漫过十七岁》倒计时
前端·javascript·后端
Wellijohn12 小时前
dict、tuple、dataclass 还是 slots?我把 6 种数据载体的 8 项开销全测了一遍
后端·python·性能优化
HarmonSir13 小时前
OpenManus 的架构解析:另一个通用 Agent 的最小内核与扩展边界
后端·架构
hsfxuebao13 小时前
Sandbox(沙箱)到底是什么?
后端
一条Go13 小时前
proto 契约:校验注解、错误码与「文案即接口」
后端·github