假设我有这段代码:
fn non_zero_rand() -> i32 {
let x = rand();
match x {
0 => 1,
_ => x,
}
}
有没有一种简洁的方法将 rand()
放入匹配中,然后将其绑定到一个值。例如,类似于这样的写法:
fn non_zero_rand() -> i32 {
match let x = rand() {
0 => 1,
_ => x,
}
}
或者也可以是:
fn non_zero_rand() -> i32 {
match rand() {
0 => 1,
_x => _x,
}
}
_x => _x
可以工作,但它在语义上不正确。前导下划线表示“此值未使用”,因此在这里不合适。 - Shepmaster