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 小时前
Linux系统编程—基础IO
linux·运维·服务器·c语言·c++·人工智能·算法
Shingmc31 小时前
【Linux】数据链路层
linux·服务器·网络
zyk_computer1 小时前
AI 时代,或许 Rust 比 Python 更合适
人工智能·后端·python·ai·rust·ai编程·vibe coding
a752066281 小时前
零基础实操:小龙虾 AI OpenClaw 接入 Kimi 详细步骤
运维·服务器
KK溜了溜了1 小时前
Python从入门到精通
服务器·开发语言·python
念恒123062 小时前
Ext系列文件系统(下)
linux·运维·服务器
小贾要学习2 小时前
【Linux】Linux高性能IO多路复用:epoll全方位详解(从原理到实战)
linux·服务器·网络
Rust语言中文社区3 小时前
【Rust日报】2026-05-14 Pyrefly v1.0 正式发布:快速的 Python 类型检查器和语言服务器
开发语言·后端·python·rust
2601_956139423 小时前
广州VI设计公司哪家强
linux·运维·服务器·python