rust - 从函数返回未来生成的闭包
问题描述
检查以下代码时:
pub fn producer<F>() -> impl Fn() -> F
where
F: std::future::Future<Output = ()>,
{
|| async move {
println!("Hello, World!");
}
}
我收到以下编译器错误:
error[E0308]: mismatched types
--> src/lib.rs:5:8
|
1 | pub fn producer<F>() -> impl Fn() -> F
| - this type parameter
...
5 | || async move {
| ________^
6 | | println!("Hello, World!");
7 | | }
| |_____^ expected type parameter `F`, found opaque type
|
::: /Users/tf/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/future/mod.rs:61:43
|
61 | pub const fn from_generator<T>(gen: T) -> impl Future<Output = T::Return>
| ------------------------------- the found opaque type
|
= note: expected type parameter `F`
found opaque type `impl Future`
= help: type parameters must be constrained to match other types
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
For more information about this error, try `rustc --explain E0308`.
但是,未来应该最终解决单元。我已经看到了多条错误消息,基本上都与类型不匹配有关,而我怀疑类似丢失Sized
标记的东西。但话又说回来,拳击封闭也无济于事。
常见问题:
- 是什么让编译器无法接受我的闭包作为正确的类型?
- 我该如何解决?
解决方案
推荐阅读
- powershell - 如何检查powershell中的参数数量?
- reactjs - 如何通过定位文件 REACT 来渲染不同的图像源
- python - import statistics in visual studio code using Python 3.8 cannot find median
- spring-cloud-dataflow - 如何在 Spring Cloud Data Flow 中保存日志文件?
- maven - 如何配置 Intellij 日志级别以调试 Maven 输出
- python-3.x - 执行 conda install notebook 时出现 InvalidArchiveError
- c# - 如何导航到 Blazor 应用程序中的 ASP.NET Core MVC 控制器?
- sql - 如何将数据从 SSIS 源导出到 Google 电子表格
- mongodb - 如何决定何时在生产中为 mongodb 使用复制集
- android - 未使用 RoomDB 插入多行数据