2
我想要实现的一些具体功能的自定义特征,即实施性状FN型
trait ToTarget {
fn custom_str(&self) -> String;
}
impl ToTarget for fn() -> String {
fn custom_str(&self) -> String {
self()
}
}
impl ToTarget for fn(i32) -> String {
fn custom_str(&self) -> String {
self(4)
}
}
fn a() -> String {
"abc".to_string()
}
fn b(x: i32) -> String {
x.to_string()
}
fn main() {
println!("{}", b.custom_str());
}
然而,这并不编译给下一个错误:
<anon>:26:22: 26:34 error: no method named `custom_str` found for type `fn(i32) -> collections::string::String {b}` in the current scope
<anon>:26 println!("{}", b.custom_str());
^~~~~~~~~~~~
note: in expansion of format_args!
<std macros>:2:25: 2:56 note: expansion site
<std macros>:1:1: 2:62 note: in expansion of print!
<std macros>:3:1: 3:54 note: expansion site
<std macros>:1:1: 3:58 note: in expansion of println!
<anon>:26:5: 26:36 note: expansion site
<anon>:26:22: 26:34 help: items from traits can only be used if the trait is implemented and in scope; the following trait defines an item `custom_str`, perhaps you need to implement it:
<anon>:26:22: 26:34 help: candidate #1: `ToTarget`
error: aborting due to previous error
playpen: application terminated with error code 101
但是,如果我指定b代码编译的类型:
println!("{}", (b as fn(i32) -> String).custom_str());
所以问题是:有没有办法让我的第一个版本sion of code
println!("{}", b.custom_str());
compile?每次我想要使用我的特质时指定函数的类型真的很烦人。
我不确定这会产生什么样的效果,但是确保具有相同签名的所有功能都可以强制进入该签名是一件容易的事情。 –
感谢您的回答。我的第一个尝试是为'Fn'特性实现'ToTarget',但是一旦我写了第二个实现,我发现我不能第二次实现ToTarget特性。我想我会在这种特殊情况下切换到宏。 –