Rust

E0626 Borrow Across Yield Fix

E0626 is the compiler refusing to suspend a movable coroutine while it holds a borrow of one of its own locals: the borrow would point into the coroutine's saved state, and moving that state would leave it dangling. The diagnostic names both ends of the problem - the borrow and the yield it crosses - and offers one repair in its help line: add static to make the coroutine unmovable. This guide shows the error on the shapes that draw it, that repair running, the repairs that keep the coroutine movable by removing the borrow, and the boundary around the code: a borrow that ends before the yield, and a borrow of a value outside the coroutine, are both fine.

Written December 1, 2026 · Tested with Rust 1.98.0 · 18 min read

By the end of this guide
  • The rule underneath: a movable coroutine cannot suspend holding a borrow of itself
  • How to read the diagnostic
  • The first failure: a borrow of a temporary across a yield
  • The same error across the borrowing shapes

Written December 1, 2026

Tested with Rust 1.98.0-nightly

What this guide is

Error E0626 is about a borrow that is still alive when a coroutine suspends. The error codes index opens: "This error occurs because a borrow in a movable coroutine persists across a yield point." Its erroneous example borrows a temporary String inside a coroutine, yields, and only then prints through the borrow - and that ordering, borrow first and yield second, is the whole defect.

The practical payoff is that the rule is narrower than it first looks. Coroutines are an unstable feature, so every program in this guide carries the feature attributes and builds with a nightly compiler; within that setting, E0626 fires only when three things coincide: the coroutine is movable (unmarked), the borrowed value is one of the coroutine's own locals, and the borrow is still in scope at a yield. Break any one of the three and the same code compiles - the sections below break each in turn. Neighboring codes in this area are E0521, borrowed data escaping a closure, and E0623, a lifetime mismatch between a declaration and its implementation; E0626 is specifically the borrow held across a coroutine's yield.

Build every program in this guide with rustc --edition 2021 on a nightly toolchain. In the compiler transcripts below, the file names are the bare names shown on the command line.

The rule underneath: a movable coroutine cannot suspend holding a borrow of itself

When a coroutine yields, its locals are stored in the coroutine's saved state so execution can resume where it stopped. If the coroutine is movable, that saved state can change address between the yield and the resume. A borrow of one of its own locals, still live at the yield, would be a pointer into that state - valid only as long as the state never moves. Rather than track whether a particular coroutine actually moves, the compiler refuses the shape outright.

The index states the two families of repair. Marking the coroutine static makes it unmovable, so the borrow can safely persist: "Coroutines may be either unmarked, or marked with static. If it is unmarked, then the coroutine is considered "movable". At present, it is not permitted to have a yield in a movable coroutine that occurs while a borrow is still in scope. To resolve this error, the coroutine may be marked static". Or keep the coroutine movable and remove the borrow: "If the coroutine must remain movable, for example to be used as Unpin without pinning it on the stack or in an allocation, we can alternatively resolve the previous example by removing the borrow and just storing the type by value". The diagnostic's help line offers only the first repair; the second is usually the better one when the coroutine has to stay movable, and both are shown running below.

How to read the diagnostic

In the probed set, an E0626 failure is a single error followed by a single help block. The header states the rule: "borrow may still be in use when coroutine yields". Two spans are marked: carets under the borrow expression, with the label "within this coroutine" on the coroutine itself, and dashes under the yield, labeled "possible yield occurs here". The help block then performs the static repair in miniature, showing the coroutine line with static inserted and marked with plus signs.

Read the two spans as a pair: the caret span is the borrow to remove or restructure, and the dash span is the yield it must not cross. If several yields follow the borrow, the diagnostic points at the yield the borrow actually crosses - one probed program below borrows only after a first yield and is refused at the second. If your diagnostic has no help block offering static, or names a different code, you are in a different situation; the boundary sections below cover the two nearest ones, E0277 and E0554.

The first failure: a borrow of a temporary across a yield

The smallest shape is the index's mistake in ordinary crate-level form - a coroutine that borrows a temporary String, yields, and then uses the borrow:

Rust
#![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
use std::ops::Coroutine;
use std::pin::Pin;
fn main() {
    let mut b = #[coroutine] || {
        let a = &String::new();
        yield ();
        println!("{}", a);
    };
    Pin::new(&mut b).resume(());
}
Output
error[E0626]: borrow may still be in use when coroutine yields
 --> fail_basic.rs:6:17
  |
5 |     let mut b = #[coroutine] || {
  |                              -- within this coroutine
6 |         let a = &String::new();
  |                 ^^^^^^^^^^^^^^
7 |         yield ();
  |         -------- possible yield occurs here
  |
help: add `static` to mark this coroutine as unmovable
  |
5 |     let mut b = #[coroutine] static || {
  |                              ++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0626`.

Nothing is wrong with the borrow on its own, with the yield, or with the println!. The one defect is the overlap: a is borrowed on line 6, the coroutine suspends on line 7, and a is used after the resume. A borrow that is never used after a yield does not draw this error, as the boundary section shows.

The same error across the borrowing shapes

The index's second shape is iteration: a for loop borrowing the coroutine's own vector holds that borrow for the whole loop body, so every yield inside the body crosses it:

Rust
#![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
use std::ops::Coroutine;
use std::pin::Pin;
fn main() {
    let mut b = #[coroutine] || {
        let v = vec![1, 2, 3];
        for &x in &v {
            yield x;
        }
    };
    Pin::new(&mut b).resume(());
}
Output
error[E0626]: borrow may still be in use when coroutine yields
 --> fail_iter.rs:7:19
  |
5 |     let mut b = #[coroutine] || {
  |                              -- within this coroutine
6 |         let v = vec![1, 2, 3];
7 |         for &x in &v {
  |                   ^^
8 |             yield x;
  |             ------- possible yield occurs here
  |
help: add `static` to mark this coroutine as unmovable
  |
5 |     let mut b = #[coroutine] static || {
  |                              ++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0626`.

Here the carets sit under &v in the loop header, not under the yield x - the borrow the loop takes is the span the compiler blames. This is the shape real coroutine code hits most, because the borrow is implicit in the iteration rather than written as a let.

A mutable borrow fails the same way. Mutability is not the issue; persistence across the yield is:

Rust
#![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
use std::ops::Coroutine;
use std::pin::Pin;
fn main() {
    let mut b = #[coroutine] || {
        let mut s = String::from("a");
        let a = &mut s;
        yield ();
        a.push('b');
        println!("{}", a);
    };
    Pin::new(&mut b).resume(());
}
Output
error[E0626]: borrow may still be in use when coroutine yields
 --> fail_mut.rs:7:17
  |
5 |     let mut b = #[coroutine] || {
  |                              -- within this coroutine
6 |         let mut s = String::from("a");
7 |         let a = &mut s;
  |                 ^^^^^^
8 |         yield ();
  |         -------- possible yield occurs here
  |
help: add `static` to mark this coroutine as unmovable
  |
5 |     let mut b = #[coroutine] static || {
  |                              ++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0626`.

And the error is per-yield, not per-coroutine. A borrow taken after a first yield is refused at the second yield it spans, with the first yield nowhere in the diagnostic:

Rust
#![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
use std::ops::Coroutine;
use std::pin::Pin;
fn main() {
    let mut b = #[coroutine] || {
        yield ();
        let a = &String::new();
        yield ();
        println!("{}", a);
    };
    Pin::new(&mut b).resume(());
}
Output
error[E0626]: borrow may still be in use when coroutine yields
 --> fail_second_yield.rs:7:17
  |
5 |     let mut b = #[coroutine] || {
  |                              -- within this coroutine
6 |         yield ();
7 |         let a = &String::new();
  |                 ^^^^^^^^^^^^^^
8 |         yield ();
  |         -------- possible yield occurs here
  |
help: add `static` to mark this coroutine as unmovable
  |
5 |     let mut b = #[coroutine] static || {
  |                              ++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0626`.

In every probed failing shape the diagnostic is identical apart from the two spans: a shared borrow of a temporary, a loop's borrow of its vector, a mutable borrow, and a borrow spanning only a later yield. The failing family is one shape - a borrow of a coroutine local, still in scope at a yield, in an unmarked coroutine.

The repair the diagnostic names: mark the coroutine static

Applying the help line's change - adding static - takes the first failure to compiling and running. One part of the repair is not in the help line: a static coroutine is not Unpin, so it is pinned in place with the pin! macro before it is resumed:

Rust
#![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
use std::ops::{Coroutine, CoroutineState};
fn main() {
    let b = #[coroutine] static || {
        let a = &String::from("hello, world");
        yield ();
        println!("{}", a);
    };
    let mut b = std::pin::pin!(b);
    match b.as_mut().resume(()) {
        CoroutineState::Yielded(()) => println!("yielded"),
        CoroutineState::Complete(()) => println!("complete-early"),
    }
    match b.as_mut().resume(()) {
        CoroutineState::Yielded(()) => println!("yielded-again"),
        CoroutineState::Complete(()) => println!("complete"),
    }
}
Output
yielded
hello, world
complete

The output shows the whole lifecycle: the first resume runs to the yield and reports yielded, the second resumes past it, prints hello, world through the borrow that E0626 had refused, and reports complete. The borrow itself did not change - what changed is the guarantee behind it. An unmovable coroutine's saved state never changes address, so a pointer into it stays valid across the suspension.

Reaching for the movable coroutine's resume call instead is a different error. A static coroutine resumed through Pin::new draws E0277, because Pin::new requires Unpin:

Rust
#![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
use std::ops::Coroutine;
use std::pin::Pin;
fn main() {
    let mut b = #[coroutine] static || {
        let a = &String::from("hello, world");
        yield ();
        println!("{}", a);
    };
    Pin::new(&mut b).resume(());
}
Output
error[E0277]: `{static coroutine@static_pinnew.rs:5:30: 5:39}` cannot be unpinned
  --> static_pinnew.rs:10:14
   |
10 |     Pin::new(&mut b).resume(());
   |     -------- ^^^^^^ the trait `Unpin` is not implemented for `{static coroutine@static_pinnew.rs:5:30: 5:39}`
   |     |
   |     required by a bound introduced by this call
   |
   = note: consider using the `pin!` macro
           consider using `Box::pin` if you need to access the pinned value outside of the current scope
note: required by a bound in `Pin::<Ptr>::new`
  --> /rustc/c397dae808f70caebab1fc4e11b3edf7e59f58c7/library/core/src/pin.rs:1181:4

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.

So the static repair is a pair of changes, not one: mark the coroutine, and pin it where it lives - pin! on the stack, or Box::pin when the pinned coroutine has to leave the current scope, as the note in the diagnostic itself suggests.

The repair that keeps it movable: remove the borrow

The index's alternative keeps the coroutine unmarked and stores the value by value instead of borrowing it. The same program as the first failure, with the reference removed, compiles and runs:

Rust
#![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
use std::ops::{Coroutine, CoroutineState};
use std::pin::Pin;
fn main() {
    let mut b = #[coroutine] || {
        let a = String::from("hello, world");
        yield ();
        println!("{}", a);
    };
    match Pin::new(&mut b).resume(()) {
        CoroutineState::Yielded(()) => println!("yielded"),
        CoroutineState::Complete(()) => println!("complete-early"),
    }
    match Pin::new(&mut b).resume(()) {
        CoroutineState::Yielded(()) => println!("yielded-again"),
        CoroutineState::Complete(()) => println!("complete"),
    }
}
Output
yielded
hello, world
complete

The output is the same lifecycle as the static version - yielded, then hello, world, then complete - and the coroutine is still resumed through Pin::new, which is only possible because it stayed movable and therefore Unpin. Owning the value costs a stored String in the coroutine's state instead of a reference; where several places need to share the value, the index points at Rc or Arc, which keep the shared access while the coroutine owns its handle.

The iteration failure repairs the same way, by iterating by value. Consuming the vector with for x in v takes ownership of each element instead of borrowing the vector for the loop's duration:

Rust
#![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
use std::ops::{Coroutine, CoroutineState};
use std::pin::Pin;
fn main() {
    let mut b = #[coroutine] || {
        let v = vec![1, 2, 3];
        for x in v {
            yield x;
        }
    };
    loop {
        match Pin::new(&mut b).resume(()) {
            CoroutineState::Yielded(x) => println!("yielded {}", x),
            CoroutineState::Complete(()) => { println!("complete"); break; }
        }
    }
}
Output
yielded 1
yielded 2
yielded 3
complete

Each resume yields the next element - yielded 1, yielded 2, yielded 3 - and the fourth reports complete. When the vector itself must survive the loop, the index's other repair reads it by index instead of borrowing it: copy the element out, then yield the copy:

Rust
#![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
use std::ops::{Coroutine, CoroutineState};
use std::pin::Pin;
fn main() {
    let mut b = #[coroutine] || {
        let v = vec![1, 2, 3];
        let len = v.len();
        for i in 0..len {
            let x = v[i];
            yield x;
        }
    };
    loop {
        match Pin::new(&mut b).resume(()) {
            CoroutineState::Yielded(x) => println!("yielded {}", x),
            CoroutineState::Complete(()) => { println!("complete"); break; }
        }
    }
}
Output
yielded 1
yielded 2
yielded 3
complete

The output is identical - yielded 1, yielded 2, yielded 3, complete. The index's version of this shape binds the length to a len temporary before the loop; what matters for E0626 is that no borrow of v is in scope at the yield x - v[i] copies the element out and the borrow that read it ends with the statement.

The boundary: a borrow that ends before the yield is fine

E0626 is about a borrow in scope at the yield, not about borrowing anywhere in a movable coroutine. Confining the borrow to a block that closes before the yield compiles and runs in an unmarked coroutine:

Rust
#![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
use std::ops::{Coroutine, CoroutineState};
use std::pin::Pin;
fn main() {
    let mut b = #[coroutine] || {
        let s = String::from("hi");
        {
            let a = &s;
            println!("{}", a);
        }
        yield ();
        println!("after");
    };
    match Pin::new(&mut b).resume(()) {
        CoroutineState::Yielded(()) => println!("yielded"),
        CoroutineState::Complete(()) => println!("complete-early"),
    }
    match Pin::new(&mut b).resume(()) {
        CoroutineState::Yielded(()) => println!("yielded-again"),
        CoroutineState::Complete(()) => println!("complete"),
    }
}
Output
hi
yielded
after
complete

The first resume prints hi through the borrow, reaches the yield with the borrow already out of scope, and reports yielded; the second prints after and completes. When a borrow is used only before the suspension point, scoping it - or simply finishing with it before the yield - is the smallest repair of all, smaller than either restructuring above.

The boundary: a borrow of a value outside the coroutine is fine

The refused borrows all point at the coroutine's own locals. A borrow of a value that lives outside the coroutine points outside the saved state, so moving the coroutine cannot invalidate it - and in the probed set it crosses a yield freely. Here the coroutine captures a reference to a String owned by main, by move, and prints through it after the yield:

Rust
#![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
use std::ops::{Coroutine, CoroutineState};
use std::pin::Pin;
fn main() {
    let s = String::from("outside");
    let r = &s;
    let mut b = #[coroutine] move || {
        yield ();
        println!("{}", r);
    };
    match Pin::new(&mut b).resume(()) {
        CoroutineState::Yielded(()) => println!("yielded"),
        CoroutineState::Complete(()) => println!("complete-early"),
    }
    match Pin::new(&mut b).resume(()) {
        CoroutineState::Yielded(()) => println!("yielded-again"),
        CoroutineState::Complete(()) => println!("complete"),
    }
    println!("{}", s);
}
Output
yielded
outside
complete
outside

The coroutine yields, prints outside through the captured borrow after resuming, and completes - and main still prints outside from the original String afterward. So the question E0626 asks is not "is a borrow live at this yield" but "does that borrow point into this coroutine". The first is refused; the second is ordinary code.

The boundary: on the stable channel, E0554 arrives with it

Coroutines are nightly-only, and that shows up before E0626 does. The first failure's program, compiled with the stable compiler, draws two errors: E0554 for the feature attributes, and E0626 itself, reported in full underneath:

Output
error[E0554]: `#![feature]` may not be used on the stable release channel
 --> fail_basic.rs:1:1
  |
1 | #![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0626]: borrow may still be in use when coroutine yields
 --> fail_basic.rs:6:17
  |
5 |     let mut b = #[coroutine] || {
  |                              -- within this coroutine
6 |         let a = &String::new();
  |                 ^^^^^^^^^^^^^^
7 |         yield ();
  |         -------- possible yield occurs here
  |
help: add `static` to mark this coroutine as unmovable
  |
5 |     let mut b = #[coroutine] static || {
  |                              ++++++

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0554, E0626.
For more information about an error, try `rustc --explain E0554`.

Two readings follow. First, seeing E0626 in stable output does not mean the program is one fix away from building on stable: the borrow repair clears E0626, and E0554 remains until the code moves to nightly (or the feature stabilizes). Second, the borrow check runs even under the feature error, so the pair of codes separates cleanly: E0554 is about the channel, E0626 about the borrow, and each is fixed in its own place.

Fixes in reach order

First, check whether the borrow needs to cross the yield at all. If the borrowed value is only used before the suspension, end the borrow first - scope it in a block, or finish the use and let the borrow lapse before the yield. In the probed set that change alone took the scoped-borrow program to compiling and running, with the coroutine untouched and still movable.

Second, if the value is needed after the yield, store it by value. Drop the reference and keep the owned value in the coroutine's state; for iteration, consume with for x in v or copy elements out by index. This keeps the coroutine movable and Unpin, so existing Pin::new resume calls keep working. Where the value must be shared rather than owned outright, hold an Rc or Arc handle in the coroutine instead, as the index suggests.

Third, if the design genuinely needs a borrow of a local held across a yield - several live references into the coroutine's own state - mark the coroutine static and pin it: pin! where it is created, or Box::pin if it must move to the heap or out of the scope. Apply both halves together; marking without pinning trades E0626 for E0277 at the resume call.

Last, check the channel before chasing the borrow. If the build is on stable, E0554 is in the output alongside everything else, and no borrow repair makes the program build there - move the build to nightly first, then apply the fixes above to what remains.

Decision summary

E0626, in this guide's probed set, fired exactly when a borrow of a coroutine's own local was still in scope at a yield in an unmarked (movable) coroutine: a shared borrow of a temporary String, a for loop's borrow of its own vector, a mutable borrow, and a borrow spanning only the second of two yields. Every diagnostic was a single error headed "borrow may still be in use when coroutine yields", carets under the borrow, dashes under the crossed yield, and a help block offering to add static. Compiling and running were: the same coroutine marked static and pinned with pin!; the borrowed value stored by value instead; iteration by value and iteration by index; a borrow scoped to end before the yield; and a borrow of a value outside the coroutine captured across a yield. A static coroutine resumed through Pin::new drew E0277, and the first failure compiled on the stable channel drew E0554 alongside E0626.

Checklist

  • Read the two spans as a pair: the carets mark the borrow, the dashes mark the yield it must not cross.
  • Ask where the borrow points: into the coroutine's own locals is E0626 territory; a value outside the coroutine is not.
  • End borrows that are only needed before the yield - scope them, and the movable coroutine compiles as written.
  • Prefer storing by value (or iterating by value, or by index) when the coroutine must stay movable and Unpin.
  • Mark static only when a borrow of a local truly must persist - and pin in the same pass (pin! or Box::pin), or the resume call draws E0277.
  • Separate the channel error from the borrow error: on stable, E0554 stays until the build moves to nightly, whatever the borrow does.

Sources

  • Rust error codes index, E0626 - a borrow in a movable coroutine persists across a yield point; the erroneous example borrowing a temporary String across a yield, the statement that an unmarked coroutine is movable and may not yield while a borrow is in scope, the static repair, the by-value repair for coroutines that must remain movable, the iteration shapes and their by-value and by-index repairs, and the note that Rc or Arc may serve where several references to the value are needed.
  • std::ops::Coroutine trait documentation - coroutines as a nightly-only experimental API; the resume method taking Pin<&mut Self> and returning CoroutineState, where Yielded means the coroutine reached a suspension point and is available for resumption and Complete means it has finished and must not be resumed again.

Keep Exploring