syntax - 为什么我不能声明具有元组参数匹配的特征函数?
问题描述
为什么我不能声明具有元组参数匹配的特征函数?
#![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 可以改变它。
在一个特征中,除了可能用于文档之外,它没有其他用途。但是,由于无论如何您都在定义特征,因此您可以首先为该参数定义一个更具描述性的类型。在您的情况下,Point
带有x
和y
字段。
推荐阅读
- jackson - RestAssured - 无法识别的字段,未标记为可忽略
- android - 我的应用程序在启动后崩溃。android studio 上的代码显示没有错误。应用程序安装正确但无法启动
- c# - 文化不变的 StringFormat 不起作用
- python - Python Tkinter 中扩展列的问题
- python - 如何正确键入装饰器和装饰功能?
- linux - Bash 脚本 - 删除旧文件
- sql - 使用编码选择数据的 SQL
- javascript - 稳定 touchmove 上的触摸事件坐标
- c# - 如何识别返回和/或接受动态类型的成员
- django - 从 Django 视图将值传递给 CSS 样式标签