Rust:@ 符号助你从容面对 match

@符号在Rust中是一个非常有用的特性,尤其是在需要在模式匹配中绑定变量时。以下是一些使用@的例子,展示了它在不同场景下的应用。

1. 绑定枚举变体的值

假设有一个枚举表示HTTP状态代码:

rust 复制代码
#[derive(Debug)]
enum HttpStatus {
    Ok,
    NotFound,
    Unauthorized,
    Unknown(u16), // 未知的状态代码
}

let status = HttpStatus::Unknown(501);

match status {
    HttpStatus::Ok => println!("Request succeeded"),
    code @ HttpStatus::Unknown(_) => {
        println!("Unknown status code encountered: {:?}", code);
    }
    _ => println!("Some other status"),
}

在这个例子中,我们使用@来绑定匹配到的HttpStatus::Unknown变体到变量code,这样我们就可以在打印消息中使用它了。

code @部分将整个匹配的枚举值绑定到变量code上,使我们能在后续的代码块中使用code变量。这里,如果status是一个Unknown变体,不管里面的数值是多少,都会执行打印操作,打印出code,即Unknown及其携带的数值。

2. 范围匹配并绑定值

当需要对一个范围内的值进行模式匹配并在匹配的分支中使用该值时,@也很有用:

rust 复制代码
let number = 9;

match number {
    n @ 1..=10 => println!("The number {} is between 1 and 10", n),
    _ => println!("The number is not in the range 1 to 10"),
}

这个例子演示了如何检查一个数字是否位于1到10之间,并在确认后打印出来。

3. 解构结构体并绑定整个结构体

如果你想要在模式匹配时解构一个结构体的一部分字段,同时又想保留对整个结构体的引用,@符号就非常有用:

rust 复制代码
#[derive(Debug)]
struct Point {
    x: i32,
    y: i32,
}

let point = Point { x: 0, y: 7 };

match point {
   p @ Point { x, y: 0..=10 }  => {
        println!("Point is in range, x: {}, y: {}. Point: {:?}", x, p.y, p);
    }
    _ => println!("Point is out of range"),
}

这里,Point { x, y: 0..=10 } @ p不仅匹配了一个y值在0到10之间的点,而且还让我们能够通过p来引用整个Point实例。

4. 在模式守卫中使用

@也可以和模式守卫(if后面的条件表达式)结合使用,以提供更复杂的匹配逻辑:

rust 复制代码
let number = Some(42);

match number {
    Some(n @ 40..=50) if n % 2 == 0 => println!("The number is in the range and even: {}", n),
    _ => println!("The number does not match"),
}

在这个例子中,我们检查number是否是一个在40到50之间的偶数,并且只在满足这两个条件时打印信息。

相关推荐
DongLi0119 小时前
rustlings 学习笔记 -- exercises/05_vecs
rust
番茄灭世神2 天前
Rust学习笔记第2篇
rust·编程语言
shimly1234562 天前
(done) 速通 rustlings(20) 错误处理1 --- 不涉及Traits
rust
shimly1234562 天前
(done) 速通 rustlings(19) Option
rust
@atweiwei2 天前
rust所有权机制详解
开发语言·数据结构·后端·rust·内存·所有权
shimly1234562 天前
(done) 速通 rustlings(24) 错误处理2 --- 涉及Traits
rust
shimly1234562 天前
(done) 速通 rustlings(23) 特性 Traits
rust
shimly1234562 天前
(done) 速通 rustlings(17) 哈希表
rust
shimly1234562 天前
(done) 速通 rustlings(15) 字符串
rust
shimly1234562 天前
(done) 速通 rustlings(22) 泛型
rust