【发布时间】:2017-10-30 00:18:10
【问题描述】:
我想和FnMut(&[f32]) -> f32一起工作,
为了不复制/粘贴完整签名,我想介绍某种别名,但是
type Boo = FnMut(&[f32]) -> f32;
fn f<F: Boo>(mut f: F) {}
导致编译器错误:
error[E0404]: expected trait, found type alias `Boo`
--> src/main.rs:3:13
|
3 | fn f<F: Boo>(mut f: F) {}
| ^^^ type aliases cannot be used for traits
然后我尝试了:
trait Boo: FnMut(&[f32]) -> f32 {}
fn f<F: Boo>(mut f: F) {}
它已编译,但如果我尝试在另一个地方使用 Boo 代替 trait:
trait Boo: FnMut(&[f32]) -> f32 {}
struct X(Vec<Box<Boo>>);
我明白了:
error[E0191]: the value of the associated type `Output` (from the trait `std::ops::FnOnce`) must be specified
--> src/main.rs:3:18
|
3 | struct X(Vec<Box<Boo>>);
| ^^^ missing associated type `Output` value
有什么方法可以创建我可以使用的特定FnMut 的别名
而不是FnMut(&[f32]) -> f32?
【问题讨论】:
标签: syntax rust traits type-alias