我有一个闭包类型的别名:
type ClosureType = Box<Fn(i32) -> i32>;
这个 trait:
trait Trait {
fn change(&self, y: i32) -> i32;
}
并包括以下功能:
fn with_one(x: Box<Fn(i32) -> i32>) -> i32 {
x(1)
}
fn plus_one(x: i32) -> i32 {
x+1
}
fn main() {
let a = Box::new(|x: i32|{x+1});
let b: ClosureType = Box::new(|x: i32|{x+1});
let c = Box::new(plus_one);
let d: ClosureType = Box::new(plus_one);
println!("{}", a.change(1));
println!("{}", b.change(1));
println!("{}", c.change(1));
println!("{}", d.change(1));
println!("{}", with_one(a));
println!("{}", with_one(b));
println!("{}", with_one(c));
println!("{}", with_one(d));
}
当我为`ClosureType`或者`Box i32>`这个类型别名实现特质`Trait`时,需要按照以下格式书写代码:
impl Trait for ClosureType {
fn change(&self, y: i32) -> i32{
self(y)
}
}
或者
impl Trait for Box<Fn(i32) -> i32> {
fn change(&self, y: i32) -> i32{
self(y)
}
}
对于变量a
,我得到:
<anon>:32:22: 32:31 error: no method named `change` found for type
`Box<[closure <anon>:28:22: 28:35]>` in the current scope
<anon>:32 println!("{}", a.change(1));
对于变量c
,我得到:
<anon>:34:22: 34:31 error: no method named `change` found for type
`Box<fn(i32) -> i32 {plus_one}>` in the current scope
<anon>:34 println!("{}", c.change(1));
然而,变量
a
和c
是从函数with_one(x: Box<Fn(i32) -> i32>) -> i32
中接受的,换句话说,它们对于函数with_one
来说具有相同的类型(Box<Fn(i32) -> i32>
),但在Trait
实现中,它们的类型不同(Box<[closure <anon>:24:22: 24:35]>
和Box<fn(i32) -> i32 {plus_one}
)。我感觉我在这里漏掉了什么,但不确定是什么,请问您能否给我一些启示?
您可以在此 Rust playground中找到所有代码。
Box<Fn(i32) -> i32>
可以解决问题。 另外,对您的回答进行了一个错别字更正,在第一个代码块下是b
和d
而不是b
和c
。 - Otobo