gpt4 book ai didi

rust - 我想在 Rust 中使用 match 语句的条件来做 0... =a+5

转载 作者:行者123 更新时间:2023-12-03 11:26:02 24 4
gpt4 key购买 nike

想做
我想使用 Rust 的 match 语句在任意范围内和其他情况下以不同的方式处理变量。在这种情况下,代码看起来像这样。
适用代码

// idx is usize variable
// num is usize variabel

let res: Option<f64> = match idx {
1..=num-5 => {
Some(func())
},
_ => None,
};
我收到的错误。
error: expected one of `::`, `=>`, `if`, or `|`, found `-`
--> src/features.rs:34:22
|
34 | 1..=num-5 => Some(func()),
| ^ expected one of `::`, `=>`, `if`, or `|`


最佳答案

您可以检查比赛后卫的动态范围:

fn main() {
let idx = 6;
let num = 15;
let res = match idx {
n if (1..=num - 5).contains(&n) => Some("foo"),
_ => None,
};
println!("{:?}", res);
}
Playground

关于rust - 我想在 Rust 中使用 match 语句的条件来做 0... =a+5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66543639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com