Rust---复合数据类型之元组

目录

元组的使用

rust 复制代码
fn main() {
    // 创建一个元组
    let my_tuple : (i32, &str, f64) = (10, "hello", 3.14);
	// 打印元组中的元素
    println!("{:?}", my_tuple);
    
    // 访问元组中的元素
    let first_element = my_tuple.0; // 访问第一个元素
    let second_element = my_tuple.1; // 访问第二个元素
    let third_element = my_tuple.2; // 访问第三个元素

    println!("First element: {}", first_element);
    println!("Second element: {}", second_element);
    println!("Third element: {}", third_element);

    // 使用模式匹配解构元组
    let (a, b, c) = my_tuple;
    println!("a: {}, b: {}, c: {}", a, b, c);

    // 元组作为函数返回值
    fn get_coordinates() -> (i32, i32) {
        (3, 7)
    }

    let (x, y) = get_coordinates();
    println!("x: {}, y: {}", x, y);
}

输出结果

相关推荐
该用户已不存在1 天前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?
后端·python·rust
大卫小东(Sheldon)1 天前
写了一个BBP算法的实现库,欢迎讨论
数学·rust
christine-rr1 天前
linux常用命令(4)——压缩命令
linux·服务器·redis
東雪蓮☆1 天前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
echoarts1 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
乌萨奇也要立志学C++1 天前
【Linux】进程概念(二):进程查看与 fork 初探
linux·运维·服务器
绿箭柠檬茶2 天前
Ubuntu 服务器配置转发网络访问
服务器·网络·ubuntu
獭.獭.2 天前
Linux -- 信号【上】
linux·运维·服务器
路由侠内网穿透2 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
ZERO_pan2 天前
服务器装机遇到的问题
运维·服务器