给出了下面是片段:
use futures::stream::{self, StreamExt};
async fn from_bar(bar: &[Vec<&u8>]) {
let x = bar.iter().flat_map(|i| i.iter().map(|_| async { 42 }));
let foo: Vec<_> = stream::iter(x).collect().await;
}
#[tokio::main]
async fn main() {
for bar in vec![] {
tokio::spawn(async {
from_bar(bar).await;
});
}
}我得到以下错误:
error[E0308]: mismatched types
--> src/main.rs:11:9
|
11 | tokio::spawn(async {
| ^^^^^^^^^^^^ one type is more general than the other
|
= note: expected type `std::ops::FnOnce<(&&u8,)>`
found type `std::ops::FnOnce<(&&u8,)>`
error: implementation of `std::iter::Iterator` is not general enough
--> src/main.rs:11:9
|
11 | tokio::spawn(async {
| ^^^^^^^^^^^^ implementation of `std::iter::Iterator` is not general enough
|
= note: `std::iter::Iterator` would have to be implemented for the type `std::slice::Iter<'0, &u8>`, for any lifetime `'0`...
= note: ...but `std::iter::Iterator` is actually implemented for the type `std::slice::Iter<'1, &u8>`, for some specific lifetime `'1`我期待没有错误,因为生命对我来说是正确的。请注意,删除main()或删除from_bar()中的代码都可以消除错误。不仅如此,错误信息也很奇怪。它们可能与编译器中的回归有关,尽管它们似乎位于错误的位置(可能是相关的)。
版本rustc 1.43.0 (4fb7144ed 2020-04-20)
[dependencies]
futures = '0.3.1'
[dependencies.tokio]
version = '0.2'
features = ['full']发布于 2020-04-29 06:34:34
稍微简单一些的复制:
use futures::stream::{self, StreamExt};
async fn from_bar(bar: &[Vec<&u8>]) {
let x = bar.iter().flat_map(|i| i.iter().map(|_| async { 42 }));
let foo: Vec<_> = stream::iter(x).collect().await;
}
#[tokio::main]
async fn main() {
tokio::spawn(async {
from_bar(&vec![]).await;
});
}游乐场。
这个例子更清楚地说明了错误之处:传递给from_bar的引用只存在于main结束之前(甚至可能是示例中的当前循环迭代),但是spawn需要它活得更长,因为生成的任务可能运行得更长。
我不明白的是,这是如何转化为我们得到的…错误消息
https://stackoverflow.com/questions/61491070
复制相似问题