是否可以在不使用类型参数的情况下将 Arc<RwLock<&Fn()>> 传递给函数?

Is it possible to pass an Arc<RwLock<&Fn()>> to a function without using a type parameter?

我必须将 Arc<RwLock<&Fn()>> 传递给函数:

use std::sync::{Arc, RwLock};

fn main() {
    let closure = || println!("Hello World");
    let wrapped_closure = Arc::new(RwLock::new(&closure));
    execute(wrapped_closure);
}

fn execute(f: Arc<RwLock<&Fn()>>) {
    let rw_lock_read_guard = f.read().unwrap();
    (rw_lock_read_guard)()
}

Rust Playground

编译失败并显示错误消息:

error[E0308]: mismatched types
 --> src/main.rs:6:13
  |
6 |     execute(wrapped_closure);
  |             ^^^^^^^^^^^^^^^ expected trait std::ops::Fn, found closure
  |
  = note: expected type `std::sync::Arc<std::sync::RwLock<&std::ops::Fn()>>`
             found type `std::sync::Arc<std::sync::RwLock<&[closure@src/main.rs:4:19: 4:45]>>`

闭包不是Fn吗?

我试过:

是否可以在不添加类型参数和 where 子句的情况下在 Rust 中传递 Arc<RwLock<&Fn()>>?如果是,如何?

解决方案由 Shepmaster 提供并摘自评论。

诀窍是将闭包引用转换为 &Fn,将 Arc::new(RwLock::new(&closure)) 替换为 Arc::new(RwLock::new(&closure as &Fn())).