04-元组

元组 — Rust By Practice

内容来源 · Rust By Practice / Rust 语言实战

原文链接: https://practice-rust-zh.beatai.org/compound-types/tuple.html

元组

  1. 🌟 元组中的元素可以是不同的类型。元组的类型签名是 (T1, T2, ...), 这里 T1, T2 是相对应的元组成员的类型.
1
2
3
4
5
6
7
8
fn main() {
    let _t0: (u8,i16) = (0, -1);
    // 元组的成员还可以是一个元组
    let _t1: (u8, (i16, u32)) = (0, (-1, 1));
    // 填空让代码工作
    // let t: (u8, __, i64, __, __) = (1u8, 2u16, 3i64, "hello", String::from(", world"));
    let _t: (u8, u16, i64, &str, String) = (1u8, 2u16, 3i64, "hello", String::from(", world"));
}
  1. 🌟 可以使用索引来获取元组的成员
1
2
3
4
5
6
7

// 修改合适的地方,让代码工作
fn main() {
    let t = ("i", "am", "sunface");
    // assert_eq!(t.1, "sunface");
    assert_eq!(t.2, "sunface");
}
  1. 🌟 过长的元组无法被打印输出
1
2
3
4
5
6
// 修复代码错误
fn main() {
    // let too_long_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
    let too_long_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
    println!("too long tuple: {:?}", too_long_tuple);
}
  1. 🌟 使用模式匹配来解构元组
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
fn main() {
    let tup = (1, 6.4, "hello");

    // 填空
    // let __ = tup;
    let (x,z,y) = tup;

    assert_eq!(x, 1);
    assert_eq!(y, "hello");
    assert_eq!(z, 6.4);
}
  1. 🌟🌟 解构式赋值
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
fn main() {
    let (x, y, z);

    // 填空
    // __ = (1, 2, 3);
    (y, z, x) = (1, 2, 3);

    assert_eq!(x, 3);
    assert_eq!(y, 1);
    assert_eq!(z, 2);
}
  1. 🌟🌟 元组可以用于函数的参数和返回值
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13

fn main() {
    // 填空,需要稍微计算下
    // let (x, y) = sum_multiply(__);
    let (x, y) = sum_multiply((2,3));
    
    assert_eq!(x, 5);
    assert_eq!(y, 6);
}

fn sum_multiply(nums: (i32, i32)) -> (i32, i32) {
    (nums.0 + nums.1, nums.0 * nums.1)
}

你可以在这里找到答案(在 solutions 路径下)

最后修改 August 21, 2026: 更新 (76fc81a2e)