相关疑难解决方法(0)

在 Rust 未来的特征绑定中,“静态生命周期”意味着什么?

我以为我已经得到了一生'static,但现在我不确定了。

我正在尝试了解如何与 Tokio 和 Futures 合作。我的应用程序正在运行,但结构很糟糕,所以我需要分解它。这是'static我关闭的要求,我不知道如何解决。

例如,在一个main函数中,我有一个句柄并且能够spawn对其循环​​进行 future 操作:

let mut core = tokio_core::reactor::Core::new().unwrap();
let handle = core.handle();
let future = {
    ok(())
};
Run Code Online (Sandbox Code Playgroud)

它编译;现在我想移出一些逻辑:

struct Test;
impl Test {
    fn test(&self, handle: tokio_core::reactor::Handle) {
        let future = {
            ok(())
        };
        handle.spawn(future);
    }
}
Run Code Online (Sandbox Code Playgroud)

它也能编译。当我的结构变得更加复杂时:

struct Test<'a> {
    api: &'a Arc<Api>,
}

impl<'a> Test<'a> {
    fn new(api: &'a Arc<Api>) -> Self {
        Test {
            api: api,
        }
    }

    fn test(&self, msg: &telegram_bot::types::Message, …
Run Code Online (Sandbox Code Playgroud)

lifetime rust rust-tokio

5
推荐指数
0
解决办法
4904
查看次数

标签 统计

lifetime ×1

rust ×1

rust-tokio ×1