E0728 Await Outside Async Fix
Written November 27, 2026 · Tested with Rust 1.99.1 · 21 min read
- The error in fn main
- The error in a plain function
- The error in a plain closure
- Not this error: a plain block inside an async function
Written November 27, 2026
Tested with Rust 1.99.1
What this guide is
E0728 is the error for an await expression written where there is no async context to suspend. await pauses the computation it appears in until a future is ready, and only an async context — an async fn, an async block, or an async closure — is a computation that can be paused and resumed that way. A plain function, a plain closure, and fn main are not, so an await inside any of them stops compilation with E0728, `await` is only allowed inside `async` functions and blocks, with the compiler marking the enclosing item "this is not async" and the await itself as "only allowed inside async functions and blocks".
The repairs are about supplying the missing context, and there are two shapes. Make the function that contains the await an async fn, which turns it into a function that returns a future, and drive that future to completion from the synchronous code that calls it. Or leave the function alone, wrap the await in an async block, and drive the block's future. Either way something outside the async context has to poll the future — in real programs that is the async runtime's executor, and in the runnable fixes below it is a short hand-written block_on built from std::task, so every fix compiles and runs with rustc alone. The sections below take the shapes in order: the error in fn main, in a plain function, and in a plain closure; the nested-block case that looks like this error and is not; the two repairs; and the neighboring errors E0609, E0277, and E0752, which involve await but are not this one.
The error in fn main
The most common first encounter is in main, because main is where a first async experiment gets written and main cannot be async:
fn main() {
let x = std::future::ready(42).await;
println!("{}", x);
}
$ rustc --edition 2021 main.rs -o main.bin
error[E0728]: `await` is only allowed inside `async` functions and blocks
--> main.rs:2:36
|
1 | fn main() {
| --------- this is not `async`
2 | let x = std::future::ready(42).await;
| ^^^^^ only allowed inside `async` functions and blocks
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0728`.
The diagnostic has two spans and they state the whole fault. The await keyword is underlined as "only allowed inside async functions and blocks", and the signature line of the enclosing function is underlined as "this is not async". Nothing about the future being awaited is faulted: std::future::ready(42) is a perfectly good future, and the same expression inside an async context compiles. The fault is purely positional — where the await sits, not what it awaits. That is why no edit to the awaited expression repairs this error; every repair moves the await into an async context or makes its context async.
The error in a plain function
The same fault one level down, in an ordinary helper function, prints the same diagnostic with the spans on that function:
fn get() -> i32 {
std::future::ready(42).await
}
fn main() {
println!("{}", get());
}
$ rustc --edition 2021 main.rs -o main.bin
error[E0728]: `await` is only allowed inside `async` functions and blocks
--> main.rs:2:28
|
1 | fn get() -> i32 {
| --------------- this is not `async`
2 | std::future::ready(42).await
| ^^^^^ only allowed inside `async` functions and blocks
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0728`.
This is the shape that matters for the repair, because it shows what making the function async has to change. get declares that it returns an i32 directly. An async fn get does not return an i32 directly — it returns a future whose output is an i32, and the caller receives the future, not the value. So the repair is never just adding the keyword: the call site changes with it, from using the return value to driving the returned future. The first fix below makes exactly that pair of changes.
The error in a plain closure
A closure is its own context, and a non-async closure is a non-async context even when it is written inside code that could await. This closure is defined in main, but the fault would be the same inside any plain function:
fn main() {
let c = || std::future::ready(42).await;
println!("{}", c());
}
$ rustc --edition 2021 main.rs -o main.bin
error[E0728]: `await` is only allowed inside `async` functions and blocks
--> main.rs:2:39
|
2 | let c = || std::future::ready(42).await;
| -- this is not `async` ^^^^^ only allowed inside `async` functions and blocks
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0728`.
Here both spans land on the one line: the closure's parameter list is marked "this is not async" and the await is marked with the rule. The repair for this shape is the closure form of the first fix — write async || instead of ||. Calling an async closure returns a future rather than the value, so the call site then drives that future the same way it drives an async function's. The control case appears with the fixes below: the async || version of this closure, driven by the same block_on, compiles and prints 42.
Not this error: a plain block inside an async function
A block does not reset the context. An await inside an ordinary, unmarked block that sits inside an async fn is still inside the async function, and it compiles — the block is not a separate context the way a closure is. This program wraps the await in a plain block inside an async function and drives the function with the same block_on the fixes use:
use std::future::Future;
use std::sync::Arc;
use std::task::{Context, Poll, Wake};
fn block_on<F: Future>(future: F) -> F::Output {
struct Parker(std::thread::Thread);
impl Wake for Parker {
fn wake(self: Arc<Self>) {
self.0.unpark();
}
fn wake_by_ref(self: &Arc<Self>) {
self.0.unpark();
}
}
let waker = std::task::Waker::from(Arc::new(Parker(std::thread::current())));
let mut cx = Context::from_waker(&waker);
let mut future = Box::pin(future);
loop {
match future.as_mut().poll(&mut cx) {
Poll::Ready(value) => return value,
Poll::Pending => std::thread::park(),
}
}
}
async fn run() -> i32 {
let x = {
std::future::ready(42).await
};
x
}
fn main() {
println!("{}", block_on(run()));
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
42
The program prints 42 and exits with status 0. The dividing line is which constructs create a new context. A closure creates one, so an await inside a plain closure is outside the surrounding async context even when the closure is written inside an async fn. A block creates none, so an await inside a plain block belongs to whatever function or block encloses the block. When E0728 points into code that looks surrounded by async code, look for a closure boundary between the await and the async keyword — that boundary, not the block structure, is what the error is reporting.
The driver the fixes use: a minimal block_on
Making a context async only moves the problem: an async function returns a future, futures are lazy and do nothing until something polls them, and plain rustc links no runtime to do the polling. The fixes below therefore carry a minimal executor, a block_on that polls one future on the current thread until it is ready:
- It builds a
Wakerfrom aWakeimplementation whose wake operation unparks the current thread. - It pins the future and polls it in a loop with a
Contextcarrying that waker. - On
Poll::Readyit returns the value; onPoll::Pendingit parks the thread, and the waker unparks it when the future arranges a wakeup.
That is the whole contract an executor fulfills for a single future, and it is enough for every fix in this guide because the awaited future, std::future::ready(42), is ready on its first poll. A real program uses a runtime's block_on — the shape is the same: a synchronous function that takes a future and returns its output. The two fixes differ only in where the async context is created; both end at a block_on call in main.
First fix: make the function async and drive the future it returns
When the await belongs in the function — the function genuinely produces its result asynchronously — the repair is to mark the function async and change the caller to drive the future the call now returns. This is the plain-function error above, repaired:
use std::future::Future;
use std::sync::Arc;
use std::task::{Context, Poll, Wake};
fn block_on<F: Future>(future: F) -> F::Output {
struct Parker(std::thread::Thread);
impl Wake for Parker {
fn wake(self: Arc<Self>) {
self.0.unpark();
}
fn wake_by_ref(self: &Arc<Self>) {
self.0.unpark();
}
}
let waker = std::task::Waker::from(Arc::new(Parker(std::thread::current())));
let mut cx = Context::from_waker(&waker);
let mut future = Box::pin(future);
loop {
match future.as_mut().poll(&mut cx) {
Poll::Ready(value) => return value,
Poll::Pending => std::thread::park(),
}
}
}
async fn get() -> i32 {
std::future::ready(42).await
}
fn main() {
println!("{}", block_on(get()));
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
42
The program prints 42 and exits with status 0. Two edits did the work, and both are required. async on get makes the await inside it legal, and it changes what get() evaluates to: not 42, but a future that will produce 42 when polled. block_on(get()) in main is the polling. Leaving out the second edit — calling get() and using the result as an i32 — fails differently, as a type mismatch between the future and the integer, because nothing has run the async body yet. This propagation is normal and it terminates at exactly one place: the outermost synchronous entry point, usually main, where a block_on (or a runtime attribute that generates one) drives the top-level future. Every function between the await and that entry point either becomes async itself or receives and passes on the future.
The closure form is the same repair at smaller scale. Writing the closure from the error section as async || std::future::ready(42).await makes calling it return a future, and driving that future prints the value:
use std::future::Future;
use std::sync::Arc;
use std::task::{Context, Poll, Wake};
fn block_on<F: Future>(future: F) -> F::Output {
struct Parker(std::thread::Thread);
impl Wake for Parker {
fn wake(self: Arc<Self>) {
self.0.unpark();
}
fn wake_by_ref(self: &Arc<Self>) {
self.0.unpark();
}
}
let waker = std::task::Waker::from(Arc::new(Parker(std::thread::current())));
let mut cx = Context::from_waker(&waker);
let mut future = Box::pin(future);
loop {
match future.as_mut().poll(&mut cx) {
Poll::Ready(value) => return value,
Poll::Pending => std::thread::park(),
}
}
}
fn main() {
let c = async || std::future::ready(42).await;
println!("{}", block_on(c()));
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
42
The program prints 42 and exits with status 0.
Second fix: wrap the await in an async block and drive that
When the enclosing function should stay synchronous — it is main, or a function whose signature other synchronous code depends on — the async context can be created at the expression instead. An async block is an expression whose value is a future, so the await can move inside a block created exactly where it is needed, and the synchronous function drives that one future:
use std::future::Future;
use std::sync::Arc;
use std::task::{Context, Poll, Wake};
fn block_on<F: Future>(future: F) -> F::Output {
struct Parker(std::thread::Thread);
impl Wake for Parker {
fn wake(self: Arc<Self>) {
self.0.unpark();
}
fn wake_by_ref(self: &Arc<Self>) {
self.0.unpark();
}
}
let waker = std::task::Waker::from(Arc::new(Parker(std::thread::current())));
let mut cx = Context::from_waker(&waker);
let mut future = Box::pin(future);
loop {
match future.as_mut().poll(&mut cx) {
Poll::Ready(value) => return value,
Poll::Pending => std::thread::park(),
}
}
}
fn main() {
let value = block_on(async {
std::future::ready(42).await
});
println!("{}", value);
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
42
The program prints 42 and exits with status 0. This is the repair the official page for this error demonstrates: its corrected example pairs an async fn with a function returning impl Future whose body is an async move block containing the await. The choice between the two fixes is a design choice about where the future lives. Make the function async when being asynchronous is part of what the function is — callers should see a future in its contract. Use an async block when the asynchrony is local to one expression — a single call to collect a value at a boundary, typically at the top of main or where synchronous code meets one async library call — and the function's own contract stays synchronous. Writing async move instead of async makes the block take ownership of the values it captures, which is the form to use when the future must outlive the current stack frame, for example when it is returned or handed to a task.
Neighboring error: the same line under the 2015 edition is E0609
await became a keyword in the 2018 edition. Compiled as 2015, the identical line from the first error section is not E0728 at all, because .await is not parsed as an await expression — it is parsed as a field access named await on the future, and there is no such field:
fn main() {
let x = std::future::ready(42).await;
println!("{}", x);
}
$ rustc --edition 2015 main.rs -o main.bin
error[E0609]: no field `await` on type `std::future::Ready<{integer}>`
--> main.rs:2:36
|
2 | let x = std::future::ready(42).await;
| ^^^^^ unknown field
|
= note: to `.await` a `Future`, switch to Rust 2018 or later
= help: pass `--edition 2024` to `rustc`
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0609`.
That is E0609, no field on a type, with the header naming the future's type and the notes naming the actual fault: the edition. This printing appears when an old crate, an old --edition flag, or an old example is compiled with a current compiler, and it is worth recognizing because the fix has nothing to do with async contexts — moving the await into an async fn changes nothing under 2015, where no async context makes .await parse as an await. The repair is the edition: build as 2018 or later, and the same code then reports E0728 if the context is still wrong, which is the error this guide is about. Under 2018 and later editions this E0609 printing for .await does not occur.
Neighboring error: awaiting a value that is not a future is E0277
E0728 checks where the await is written, not what it is written on. Inside an async context the position is legal, and if the operand is not a future the failure is a trait error instead. Awaiting an integer inside an async fn:
use std::future::Future;
use std::sync::Arc;
use std::task::{Context, Poll, Wake};
fn block_on<F: Future>(future: F) -> F::Output {
struct Parker(std::thread::Thread);
impl Wake for Parker {
fn wake(self: Arc<Self>) {
self.0.unpark();
}
fn wake_by_ref(self: &Arc<Self>) {
self.0.unpark();
}
}
let waker = std::task::Waker::from(Arc::new(Parker(std::thread::current())));
let mut cx = Context::from_waker(&waker);
let mut future = Box::pin(future);
loop {
match future.as_mut().poll(&mut cx) {
Poll::Ready(value) => return value,
Poll::Pending => std::thread::park(),
}
}
}
async fn run() -> i32 {
42.await
}
fn main() {
println!("{}", block_on(run()));
}
$ rustc --edition 2021 main.rs -o main.bin
error[E0277]: `{integer}` is not a future
--> main.rs:27:8
|
27 | 42.await
| ^^^^^ `{integer}` is not a future
|
= help: the trait `Future` is not implemented for `{integer}`
= note: {integer} must be a future or must implement `IntoFuture` to be awaited
= note: required for `{integer}` to implement `IntoFuture`
help: remove the `.await`
|
27 - 42.await
27 + 42
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0277`.
That is E0277: the header says the operand "is not a future", the notes state that the value must be a future or implement IntoFuture to be awaited, and the help proposes removing the .await. The split between the two errors is exact. Wrong context, future operand: E0728. Right context, non-future operand: E0277. And the two checks are ordered — in a non-async context the compiler reports E0728 for the position without the operand's type saving or changing the verdict, so an await on a non-future in plain code is still E0728 first; fix the context, and only then does an operand problem surface as E0277.
Neighboring error: marking main itself async is E0752
The tempting one-word repair for the first error section — put async on main — is its own error:
async fn main() {
let x = std::future::ready(42).await;
println!("{}", x);
}
$ rustc --edition 2021 main.rs -o main.bin
error[E0752]: `main` function is not allowed to be `async`
--> main.rs:1:1
|
1 | async fn main() {
| ^^^^^^^^^^^^^^^ `main` function is not allowed to be `async`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0752`.
That is E0752, and the reason is the mechanism behind both fixes: main is called by the runtime environment directly, and nothing would exist to poll the future an async main returned. Async code needs an executor, and main's job in an async program is to start one — in the fixes above, by calling block_on — not to be a future itself. Some runtimes offer an attribute that can be placed on main and generates the synchronous entry point plus the executor call; plain rustc has no such attribute, and the generated shape is exactly the second fix: a synchronous main that drives an async block.
The exact rule: await needs an async context, and an async context produces a future
The official page for E0728 states the fault in one line: `await` has been used outside an async function or async block. Its erroneous example is a plain function awaiting a hand-written future, and its corrected example makes that function async and adds a second function returning impl Future whose body is an async move block containing the same await — the two repairs in this guide. The page carries no note retiring or replacing this code; E0728 is the current code for this fault.
The mechanism is in the Reference's page on await expressions: an await expression suspends a computation provided by an implementation of IntoFuture until the future is ready, and await expressions are legal only within an async context, like an async fn, an async closure, or an async block. The same page gives the reason a context is required: suspending means the surrounding computation must itself be a future that can return Poll::Pending and be polled again, and its desugaring polls the awaited future with "the task context which was supplied to the current async context when the async context itself was polled" — a plain function has no such context and no way to yield. The Book's chapter on futures and the async syntax states the other half: writing async fn is equivalent to writing a function that returns a future of the return type, futures do nothing unless they are awaited or polled, and main cannot be marked async because async code needs a runtime that main can set up but cannot be. Those three facts together are the whole error: await needs to sit inside a future-producing context, producing a future means someone outside must drive it, and the driving has to bottom out in synchronous code.
The fixes, ranked
- First, make the function that contains the
awaitanasync fn— or the closure an async closure — when the asynchrony is part of its job, and drive the returned future at the call site. This keeps the async nature visible in every signature it passes through, which is where callers need to see it. - Second, wrap the
awaitin anasyncblock when the enclosing function must stay synchronous, and drive the block's future where it is created. This is the boundary repair: it confines the future to one expression instead of changing the function's contract. - In both cases, something synchronous must drive the future: a runtime's
block_onin a real program, the hand-writtenblock_onin this guide's runnable fixes. An async context with no driver compiles, but the future is never polled and the code inside it never runs.
Which fix for which cause
- An
awaitinfn main: E0728, withmain's signature marked "this is notasync". Keepmainsynchronous and drive an async block withblock_on, or move the work into anasync fnand drive that frommain. - An
awaitin a plain helper function: E0728 at that function. Make itasync fnand update its callers to drive or propagate the future, up to the one synchronous entry point. - An
awaitin a plain closure, including one written inside anasync fn: E0728 at the closure. Make it anasyncclosure; calling it then returns a future to drive. - An
awaitin a plain block inside anasync fnorasyncblock: no error — the block inherits the enclosing async context. If E0728 points at code that looks enclosed, look for a closure boundary, not a block boundary. - The same
.awaitline compiled under the 2015 edition: E0609, "no fieldawait", with a note to switch to Rust 2018 or later. Fix the edition first; context repairs do not apply until.awaitparses as an await. - An
awaiton a value that is not a future, inside an async context: E0277, "is not a future", with a help offering to remove the.await. Remove it, or await an actual future; the context is already right. asyncwritten onfn mainitself: E0752, "mainfunction is not allowed to beasync". Remove it and drive an async block or anasync fnfrom the synchronousmain.
Checklist
- Read the two spans. E0728 always marks the
awaitwith the rule and the enclosing function or closure with "this is notasync" — the second span names the context that has to change. - Decide which construct owns the
await. Closures are their own context; blocks are not. Anawaitinside a closure inside an async function is outside the async context. - Remember that
asyncchanges the return. Anasync fnreturns a future of the written return type, so every call site changes from using a value to driving or propagating a future. - Provide the driver. Futures are lazy: until something polls the future — a runtime's
block_on, an.awaitin an enclosing async context — the body does not run. The chain of async contexts must end at one synchronous driver. - Check the edition before the context. E0609 with "no field
await" means the code is being compiled as 2015, where no context repair can work. - Check the operand after the context. Once the position is legal, an operand that is not a future is E0277, not E0728 — the two errors answer two different questions.
Related guides on this site
- E0733 Async Recursion Needs Boxing Fix
- E0091 Unused Type Parameter Fix
- E0084 Unsupported Representation Fix
- E0130 Patterns in Foreign Function Fix
Sources
- Rust Compiler Error Index, E0728 — for
awaithaving been used outside anasyncfunction orasyncblock, for the erroneous example of a plain function awaiting a future, and for the corrected example making the functionasyncand returningimpl Futurefrom anasync moveblock. - The Rust Reference, Await expressions — for an await expression suspending a computation provided by an
IntoFutureimplementation, for await expressions being legal only within an async context such as anasync fn, anasyncclosure, or anasyncblock, and for the desugaring that polls the awaited future with the task context supplied to the current async context. - The Rust Programming Language, Futures and the Async Syntax — for
async fnbeing equivalent to a function returning a future of the return type, for futures doing nothing until they are awaited or driven, formainnot being allowed to beasyncbecause async code needs a runtime, and forblock_ontaking a future and blocking until it runs to completion. - Rust Compiler Error Index, E0609 — for attempting to access a field on a type that does not have that field, the code the 2015-edition
.awaitline produces. - Rust Compiler Error Index, E0277 — for a type not implementing a trait it is required to implement, the code for awaiting a value that is not a future.
Keep Exploring
The full Rust hub \u2014 ownership, structs and enums, async, and measured comparisons.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.