[初学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);
相关推荐
John.Lewis4 小时前
C++进阶(12)附加学习:STL之空间配置器(了解)
开发语言·c++·笔记
IGAn CTOU4 小时前
王炸级更新!Spring Boot 3.4 正式发布,新特性真香!
java·spring boot·后端
柯西劝我别收敛4 小时前
Koordinator-Scheduler 调度器源码解析
后端·云原生
23471021274 小时前
4.16 学习笔记
开发语言·软件测试·python
tycooncool4 小时前
Spring中的IOC详解
java·后端·spring
014-code4 小时前
日志规范:怎么写才不算写废话
java·开发语言·设计模式·日志
303785 小时前
消息推送削峰落地方案
后端
techdashen5 小时前
每次 `cargo build` 背后,有人在默默撑着这一切
rust
Binarydog_Lee5 小时前
Rust 核心机制:所有权、借用与生命周期
开发语言·rust
XMYX-05 小时前
17 - Go 通道 Channel 底层原理 + 实战详解
开发语言·golang