Futures and 'static


#1

Can Futures (0.1) only be implemented on a struct that does not have lifetimes? (I’m fighting the compiler and not sure if there’s a direct way around this yet)

On a related note, is there a term for structs that do not have lifetimes? (I get that there’s some implicit lifetimes everywhere, but I mean - like user defined ones)


#2

Whenever you see 'static required by futures, it means references are not allowed. Instead of references, you must use move || closures and Box, Arc and other owned or copyable types.

99% of time it’s caused by use of a function’s argument or local variable inside .then(|| {}) or other futures-related closures. They can’t use temporary borrows, because the entire futures chain will start executing elsewhere, later, long after your function returns, and all its variables and parameters are destroyed.

Note that in the future Rust will get Pin types and the async/await syntax will be able to extend lifetime of local variables used in futures.