lifetime

Owned captures and the 'static future bound

A static type bound rules out borrowing short-lived external data; it does not mean the future must execute forever. An async move block can own a String and satisfy the bound. A moved reference can still have a short lifetime. The fixture-only ready_now helper polls once and rejects Pending; it is not an executor. Expected stdout (not executed): 4 Common mistake: Assuming async move extends the lifetime of every borrowed reference.

Formal Syntax & Structure

rust syntaxrust
fn require_static<F: Future + 'static>(f: F) -> F

Working Example

Example Snippetrust
fn main() {    use std::{future::Future, task::{Context, Poll, Waker}};    fn ready_now<F: Future>(f: F) -> F::Output {        let mut f = std::pin::pin!(f);        let mut cx = Context::from_waker(Waker::noop());        match f.as_mut().poll(&mut cx) {            Poll::Ready(value) => value,            Poll::Pending => panic!("fixture expected immediate readiness"),        }    }    fn require_static<F: Future + 'static>(f: F) -> F { f }    let name = String::from("Rust");    let future = require_static(async move { name.len() });    println!("{}", ready_now(future));}
Rust Reference