rust 只有当特征在范围内时,才能使用特征中的项

u7up0aaq  于 2022-11-12  发布在  其他
关注(0)|答案(1)|浏览(148)

我正在用这个例子学习未来和线程,但是这个例子没有编译,我不明白为什么。

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?

ecbunoof

ecbunoof1#

futures-cpupool需要futures版本0.1,而版本0.3.25与之不兼容。因此,降级未来:

futures = "0.1"

然而,正如@isaactfa所说,真实的的问题是futures-cpupool太老了。

相关问题