我想在Rust的頂級函數中使用一個可用的特徵。頂級函數的缺失實現
trait FnTrait {
fn call(self);
}
impl FnTrait for fn() {
fn call(self) {
self()
}
}
fn foo() {
println!("Hello, World!")
}
fn main() {
FnTrait::call(foo)
}
但是下面的代碼失敗鑄造foo
像這樣
FnTrait::call(foo as fn())
error[E0277]: the trait bound `fn() {foo}: FnTrait` is not satisfied
--> <anon>:16:5
|
16 | FnTrait::call(foo)
| ^^^^^^^^^^^^^ the trait `FnTrait` is not implemented for `fn() {foo}`
|
= help: the following implementations were found:
<fn() as FnTrait>
= note: required by `FnTrait::call`
編譯我發現我可以欺騙成編譯但它是討厭,我的程序中的一些功能比foo
更復雜。任何方法來避免演員?某種程度上我的特質錯了嗎?