在Rust 1.0之前,我可以使用这种过时的闭包语法编写一个 struct :
struct Foo {
pub foo: |usize| -> usize,
}
现在我可以做如下事情:
struct Foo<F: FnMut(usize) -> usize> {
pub foo: F,
}
但是我创造的Foo
个物体是什么类型的呢?
let foo: Foo<???> = Foo { foo: |x| x + 1 };
我也可以参考:
struct Foo<'a> {
pub foo: &'a mut FnMut(usize) -> usize,
}
我觉得这比较慢,因为
- 指针解引用
- 对于最终实际使用的
FnMut
型没有专门化