Understanding Rust Error E0626
Introduction
Rust Error E0626 occurs when a borrow in a generator persists across a yield point. In this article, we will provide an in-depth explanation for this error, its causes, and different ways to resolve it.
Erroneous code example
#![feature(generators, generator_trait, pin)]
use std::ops::Generator;
use std::pin::Pin;
let mut b = || {
let a = &String::new();
yield ();
println!("{}", a);
};
Pin::new(&mut b).resume(());
In the example above, the variable a is borrowed, which persists across the yield point. This causes the error E0626.
Solution
To solve this error, the borrow must be either contained within a smaller scope or eliminated in another way. In simple cases, you can store the value directly instead of borrowing it.
Corrected code example
#![feature(generators, generator_trait, pin)]
use std::ops::Generator;
use std::pin::Pin;
let mut b = || {
let a = 3;
yield ();
println!("{}", a);
};
Pin::new(&mut b).resume(());
In more complex cases, where you need more than one reference to the borrowed value, consider using Rc or Arc types.
Handling Iteration
Error E0626 frequently arises with iteration. Consider the following example:
#![feature(generators, generator_trait, pin)]
use std::ops::Generator;
use std::pin::Pin;
let mut b = || {
let v = vec![1,2,3];
for &x in &v {
yield x;
}
};
Pin::new(&mut b).resume(());
The error occurs because the borrow of v persists across the yield point. To resolve this issue, iterate by value using into_iter() or take ownership of the values to avoid borrowing.
Modified code example
#![feature(generators, generator_trait, pin)]
use std::ops::Generator;
use std::pin::Pin;
let mut b = || {
let v = vec![1,2,3];
for x in v {
yield x;
}
};
Pin::new(&mut b).resume(());
Alternatively, you can use indices instead of borrowing the values:
#![feature(generators, generator_trait, pin)]
use std::ops::Generator;
use std::pin::Pin;
let mut b = || {
let v = vec![1,2,3];
let len = v.len();
for i in 0..len {
let x = v[i];
yield x;
}
};
Pin::new(&mut b).resume(());
By following the above approaches, you can resolve Rust Error E0626 and ensure your generator code works correctly.