Home
Rust
Cannot Borrow Error
Updated Mar 27, 2022
Dot Net Perls
Cannot borrow error. In developing Rust programs, we may encounter a "cannot borrow as mutable" error when using an iterator. This can occur in a loop that calls iter().
In Rust, we must not modify data that we have borrowed for an iter—this would cause the iterator to work incorrectly. To fix this error, we can separate the immutable data for the iter().
Error example. Consider this program—it won't compile because the borrow checker finds an error. We try to iterate with iter() over part of the Data struct.
However We also try to borrow the Data mutably and add results to the vector in Data.
So The program fails to compile because we try to mutate (add results to) the same struct we are iterating over.
mut
Info We could fix this program by not using iter() at all, and using a for-loop. Or we could change our data structures to avoid the problem.
for
struct Data { values: Vec<u8>, results: Vec<bool> } fn add_result(data: &mut Data) { data.results.push(true); } fn main() { let mut data = Data { values: vec![1, 2, 3], results: vec![] }; for &v in data.values.iter() { if v == 1 { // Won't compile. add_result(&mut data); } } }
error[E0502]: cannot borrow data as mutable because it is also borrowed as immutable
Fixed program. We can fix the error with the borrow checker by ensuring that the immutable borrow does not overlap with the mutable borrow.
Detail We separate out the values Vector to allow it to be borrowed immutably. The Data struct can then be borrowed mutably with no problem.
struct Data { results: Vec<bool> } fn add_result(data: &mut Data) { data.results.push(true); } fn main() { // Use separate variable for the data we iterate. let values = vec![1, 2, 3]; let mut data = Data { results: vec![] }; for &v in values.iter() { if v == 1 { add_result(&mut data); } } println!("{:?}", data.results); }
[true]
A discussion. When developing Rust we must consider how our data structures are used. For data that is iterated upon, we can separate this from other data.
And This will allow the iter() to borrow immutably and not interfere with the mutable borrows elsewhere.
A summary. The Rust borrow checker can be frustrating. But by ensuring that borrowed data is not messed up elsewhere in the program, we can be assured programs will work correctly.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Mar 27, 2022 (new).
Home
Changes
© 2007-2025 Sam Allen