我忘记指定参数的类型,错误消息如下:
error: expected one of `:` or `@`, found `)`
--> src/main.rs:2:12
|
2 | fn func(arg)
| ^ expected one of `:` or `@` here
Run Code Online (Sandbox Code Playgroud)
这就提出了一个问题:@符号可以做什么?我不记得阅读有关将@符号用于任何东西的信息。我也做了一些谷歌搜索,却找不到任何东西。怎么@办?
您可以使用@符号将模式绑定到名称。如Rust参考资料所示:
let x = 1;
match x {
e @ 1 ... 5 => println!("got a range element {}", e),
_ => println!("anything"),
}
Run Code Online (Sandbox Code Playgroud)
Rust中的赋值允许模式表达式(只要它们是完整的),并且参数列表也不例外。在的特定情况下@,这不是很有用,因为您已经可以命名匹配的参数。但是,为了完整起见,下面是一个编译示例:
enum MyEnum {
TheOnlyCase(u8),
}
fn my_fn(x @ MyEnum::TheOnlyCase(_): MyEnum) {}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1052 次 |
| 最近记录: |