Borrowing across await
Quick answer
Async futures can hold references across suspension points. Such borrows must remain valid until the future no longer needs them, including while it is suspended.
Track ownership across suspension
An async function borrowing input produces a future tied to that input. Awaiting locally can preserve the lifetime relationship; spawning onto Tokio with spawn requires the task future and output to be Send + 'static. Owning a String inside async move can satisfy the lifetime boundary. A 'static bound does not require the task to live forever.
use std::{future::{Future, ready}, task::{Context, Poll, Waker}};async fn size(s: &str) -> usize { ready(()).await; s.len() }fn main() { let name = String::from("book"); let mut f = std::pin::pin!(size(&name)); let mut cx = Context::from_waker(Waker::noop()); assert_eq!(f.as_mut().poll(&mut cx), Poll::Ready(4)); println!("4");}Expected stdout (not executed):
4The owner remains alive while the local future exists.
async fn size(s: &str) -> usize { s.len() }fn main() { let name = String::from("book"); let f = size(&name); drop(name); drop(f);}Expected: compilation fails; exact diagnostic not verified.
The reference is still captured by the future.
Common mistake
Do not hold a standard blocking lock guard across await when another task may need that lock.
Try it yourself
Write an async move block owning the String and explain how this changes the borrow relationship.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Borrowing across await — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
An async function borrows local text and is awaited before the text owner drops. Is a static borrow necessarily required?
Borrowing across await — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::{future::{Future, ready}, task::{Context, Poll, Waker}}; async fn size(s: &str) -> usize { ready(()).await; s.len() } fn main() { let name = String::from("book"); let mut f = std::pin::pin!(size(&name)); let mut cx = Context::from_waker(Waker::noop()); assert_eq!(f.as_mut().poll(&mut cx), Poll::Ready(4)); println!("4"); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Borrowing across await — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust async fn size(s: &str) -> usize { s.len() } fn main() { let name = String::from("book"); let f = size(&name); drop(name); drop(f); } ``` Which outcome or interpretation best matches this example?