lambda - 如何在堆上拥有一个独特的 lambda 实例,以便在不同的消费者之间共享?
问题描述
我似乎无法规避运行 lambda 将其移出共享引用的事实。
use std::rc::Rc;
fn run_my_fn(my_fn: Rc<impl FnOnce() -> u32>) -> u32 {
(*my_fn)(); // cannot move out of an `Rc`
(my_fn.as_ref())() // cannot move out of a shared reference
}
fn main() {
let my_fn = Rc::new(|| { 123 as u32 });
let _res = run_my_fn(my_fn.clone());
}
error[E0507]: cannot move out of an `Rc`
--> src/main.rs:4:5
|
4 | (*my_fn)();
| ^^^^^^^^ move occurs because value has type `impl FnOnce() -> u32`, which does not implement the `Copy` trait
error[E0507]: cannot move out of a shared reference
--> src/main.rs:5:5
|
5 | (my_fn.as_ref())()
| ^^^^^^^^^^^^^^^^ move occurs because value has type `impl FnOnce() -> u32`, which does not implement the `Copy` trait
解决方案
推荐阅读
- python-3.x - 通过 Pandas Datareader (Python) 连接到 Iex API
- angular - 无法运行 Angular 项目 [Angular 6]
- php - 如何在 laravel 中添加“状态”、“消息”
- javascript - 如何在javascript中将二维数组转换为二维字符串
- ios - 单击单元格中的按钮时访问所有单元格
- laravel-5 - 如何从 Laravel 视图中的链接调用控制器方法?
- javascript - Chrome 扩展通知:页面加载时的 API 调用
- jms - 带有 TLS 的 Websphere MQ 的 Nifi JMS 输入
- sql - 为每个参数选择第一个结果
- python - Python只打印一个文件