我正在用这个例子学习未来和线程,但是这个例子没有编译,我不明白为什么。
use futures::future::Future;
use futures_cpupool::{CpuFuture, CpuPool};
use notify::{RecursiveMode, Watcher, watcher};
use std::{thread, time};
fn main() {
let pool = CpuPool::new(10);
let mut handles = Vec::new();
for i in 0..10 {
let i = i.clone();
let handle: CpuFuture<(), ()> = pool.spawn_fn(move || {
loop {
println!("{}", i);
}
Ok(())
});
handles.push(handle);
}
thread::sleep(time::Duration::from_secs(2));
for handle in handles {
handle.wait().unwrap();
}
}
错误:
error[E0599]: no method named `wait` found for struct `CpuFuture` in the current scope
--> src/main.rs:28:17
|
28 | handle.wait().unwrap();
| ^^^^ method not found in `CpuFuture<(), ()>`
|
::: /home/placek/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.31/src/future/mod.rs:297:8
|
297 | fn wait(self) -> result::Result<Self::Item, Self::Error>
| ---- the method is available for `CpuFuture<(), ()>` here
|
= help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
|
2 | use futures::future::Future;
|
我在范围内有Future
,所以我认为它应该工作,我还检查了CpuFuture实现Future
特征。
这是我的货
[package]
name = "folderWatcher"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
notify = "4.0.12"
futures-cpupool = "0.1.7"
futures = "0.3.25"
我还想问一下,是否需要显式启动每个Future?
1条答案
按热度按时间ecbunoof1#
futures-cpupool
需要futures
版本0.1
,而版本0.3.25
与之不兼容。因此,降级未来:然而,正如@isaactfa所说,真实的的问题是
futures-cpupool
太老了。