为什么我不能声明一个带有元组参数匹配的特征函数?
Why can't I declare a trait function with tuple parameter matching?
为什么我不能声明一个元组参数匹配的特征函数?
#![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 来更改它。
在 trait 中,除了可能用于文档之外,它没有任何用途。但是,由于您无论如何都在定义特征,因此您可以首先为该参数定义一个更具描述性的类型。在您的情况下,Point
带有 x
和 y
字段。
为什么我不能声明一个元组参数匹配的特征函数?
#![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 来更改它。
在 trait 中,除了可能用于文档之外,它没有任何用途。但是,由于您无论如何都在定义特征,因此您可以首先为该参数定义一个更具描述性的类型。在您的情况下,Point
带有 x
和 y
字段。