为什么我不能声明具有元组参数匹配的 trait 函数?



为什么我不能声明具有元组参数匹配的特征函数?

#![allow(unused)]
// This works
fn foo((x, y): (i32, i32)) {
}
trait Bar {
// This does not work
fn bar((x, y): (i32, i32));
}

游乐场

编译以上输出:

error: expected one of `)` or `,`, found `:`
--> src/main.rs:7:18
|
7 |     fn bar((x, y): (i32, i32));
|                  ^ expected one of `)` or `,` here
error: expected one of `!`, `&&`, `&`, `(`, `)`, `*`, `<`, `?`, `[`, `_`, `dyn`, `extern`, `fn`, `for`, `impl`, `unsafe`, or lifetime, found `:`
--> src/main.rs:7:18
|
7 |     fn bar((x, y): (i32, i32));
|                  ^ expected one of 17 possible tokens here
error[E0601]: `main` function not found in crate `playground`
|
= note: consider adding a `main` function to `src/main.rs`

Rust中不支持此语法,并且目前没有打开的RFC来更改此语法。

在一种特性中,它除了可能用于文档之外没有其他用途。但是,既然你无论如何都在定义特质,你可以首先为这个论点定义一个更具描述性的类型。在您的情况下,是一个具有xy字段的Point

最新更新