- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个有效的函数:
extern crate tokio;
use std::time::{Duration, Instant};
use tokio::prelude::*;
use tokio::timer::Interval;
fn run(label: String) -> impl Future<Item = (), Error = ()> {
Interval::new(Instant::now(), Duration::from_millis(1000))
.for_each(move |instant| {
println!("fire; instant={:?}, label={:?}", instant, label);
Ok(())
})
.map_err(|e| panic!("interval errored; err={:?}", e))
}
fn main() {
tokio::run(run("Hello".to_string()));
}
在这种情况下,我想创建一个包含一些参数(label
)的结构,方法 run
将利用这些参数:
extern crate tokio;
use std::time::{Duration, Instant};
use tokio::prelude::*;
use tokio::timer::Interval;
struct Ir {
label: String,
}
impl Ir {
fn new(label: String) -> Ir {
Ir { label }
}
fn run(&self) -> impl Future<Item = (), Error = ()> + '_ {
Interval::new(Instant::now(), Duration::from_millis(1000))
.for_each(move |instant| {
println!("fire; instant={:?}, label={:?}", instant, self.label);
Ok(())
})
.map_err(|e| panic!("interval errored; err={:?}", e))
}
}
fn main() {
let ir = Ir::new("Hello".to_string());
tokio::run(ir.run());
}
我得到的是:
error[E0597]: `ir` does not live long enough
--> src/main.rs:28:16
|
28 | tokio::run(ir.run());
| ^^ borrowed value does not live long enough
29 | }
| - borrowed value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
我已经阅读了“Rust by Example”中的“Advanced Lifetimes”和“Validating References with Lifetimes”,但我仍然不明白如何修复它。
为什么 ir
活得不够长?
我在尝试调用 ir.run()
的同一范围内创建了它,因此我认为它会一直存在。
最佳答案
如果你重写:
tokio::run(ir.run());
作为:
tokio::run(Ir::run(&ir))
错误变得更清楚了:
error[E0597]: `ir` does not live long enough
--> src/main.rs:28:24
|
28 | tokio::run(Ir::run(&ir));
| ^^ borrowed value does not live long enough
29 | }
| - borrowed value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
自 tokio::run
Future 需要一个'static
生命周期:
pub fn run<F>(future: F)
where
F: Future<Item = (), Error = ()> + Send + 'static,
为避免生命周期问题,请考虑使用 Ir
值:
fn run(self) -> impl Future<Item = (), Error = ()> {
Interval::new(Instant::now(), Duration::from_millis(1000))
.for_each(move |instant| {
println!("fire; instant={:?}, label={:?}", instant, self.label);
Ok(())
})
.map_err(|e| panic!("interval errored; err={:?}", e))
}
关于rust - 我怎样才能延长结构的生命周期,以便我可以用它调用 tokio::run?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53062958/
当运行这样的代码时: use futures::executor; ... pub fn store_temporary_password(email: &str, password: &str) -
我遵循了mdns Rust文档并粘贴了示例代码,但它抛出了以下错误:。以下是我拥有的代码:。依赖关系:。我遗漏了什么?我试着在网上寻找,但没有找到如何为这个用例创建一个反应堆。
假设我想与 Tokio 同时下载两个网页... 要么我可以用 tokio::spawn() 来实现这个: async fn v1() { let t1 = tokio::spawn(reqwe
我制作了一个还能显示天气的 LED 时钟。我的程序在一个循环中做了几件不同的事情,每件事都有不同的间隔: 每 50 毫秒更新一次 LED, 每 1 秒检查一次光照水平(以调整亮度), 每 10 分钟获
我制作了一个还能显示天气的 LED 时钟。我的程序在一个循环中做了几件不同的事情,每件事都有不同的间隔: 每 50 毫秒更新一次 LED, 每 1 秒检查一次光照水平(以调整亮度), 每 10 分钟获
tokio::run_async + futures 0.3 + tokio::net::UnixStream panic 。 设置 [package] name = "prac" version =
在我的 rust 项目中,cargo 提示使用 tokio::sync 时使用的类型不在范围内: use tokio::sync::RwLock; | ^^^^^ use of undec
我将如何使用自定义 tokio 运行时构建器并且没有主宏来实现这个 tokio_postgres 示例? 这工作正常,根据 tokio_postgres docs : 示例/withmacro.rs
目前我有一个主要的写成 async example for the Reqwest library . #[tokio::main] async fn main() -> Result> { 我们可以
我遵循the mdns Rust documentation并粘贴了示例代码,但它引发以下错误: thread 'main' panicked at 'there is no reactor runn
extern crate tokio; // 0.1.22 use tokio::io; use tokio::net::TcpListener; use tokio::prelude::*; use
我正在尝试使用 tokio 编写一个测试程序,该程序从网站获取文件并将流式响应写入文件。 hyper 网站显示了一个使用 while 循环并使用 .data() 的示例。方法响应主体,但我想用 .ma
我在 prod 中运行一个 rust Tokio 应用程序。在上一个版本中,我有一个错误,一些请求导致我的代码进入无限循环。 发生的事情是当进入无限循环的任务卡住时,所有其他任务继续正常工作并处理请求
下面的程序应该从多个线程定期打印,但是 tokio::time::sleep没有按我预期的那样工作: use tokio::prelude::*; //0.3.4 use tokio::runtime
我使用如下代码启动 Tokio 运行时: tokio::run(my_future); 我的 future 继续启动一堆任务以响应各种条件。 其中一项任务负责确定程序何时关闭。但是,我不知道如何让该任
我正在尝试构建一个可以管理来自 websocket 的提要但能够在多个提要之间切换的对象。 有一个 Feed 特征: trait Feed { async fn start(&mut self
我有一个设置,我的程序使用 std::thread::spawn 为 CPU 绑定(bind)计算生成多个线程。 我需要一个 GRPC 服务器来处理传入的命令并流式传输工作线程完成的输出。我正在为 G
我做计算机系统项目的第一个经历是使用 vanilla Java 构建服务器,然后在 Android 手机上构建客户端。从那时起,我发现有很多框架可以帮助管理可伸缩性并消除编写样板代码的需要。 我正在尝
我将从 Delphi XE4 迁移到 10.2。新的单位名称样式(深灰色背景上的黑色文本)不适合我的视力。有人可以建议如何更改它,最好不使用第 3 方加载项吗? 这就是新样式的样子,我很难阅读事件单位
我一直在寻找tokio源代码来获取问题的答案,并且给人以the sleep method literally puts a timer with duration的印象,但是我认为我可能误解了代码,因
我是一名优秀的程序员,十分优秀!