I'm trying to build a very basic example of an asynchronous callback function in Rust:
extern crate tokio;
extern crate tokio_core;
extern crate tokio_io;
use std::error::Error;
use tokio::prelude::future::ok;
use tokio::prelude::Future;
fn async_op() -> impl Future<Item = i32, Error = Box<Error>> {
ok(12)
}
fn main() {
let fut = async_op().and_then(|result| {
println!("result: {:?}", result);
});
tokio::run(fut);
}
This always results in the compiler error:
error[E0106]: missing lifetime specifier
--> src/main.rs:9:54
|
9 | fn async_op() -> impl Future<Item = i32, Error = Box<Error>> {
| ^^^^^ expected lifetime parameter
|
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
= help: consider giving it a 'static lifetime
Why is there a lifetime error in the first place? Why is it only for the Error
but not for the Item
?
I am also unsure about "help: consider giving it a 'static lifetime" ‒ AFAICT this would result in a lifetime of the return value over the entire program execution, which is definitely not what I would want in a more complex example.