Borrow container and mutably borrow iterated items at the same time

Consider this example:

struct Item {
    name: String,

struct Container {
    name: String,
    items: Vec<Item>,
impl Container {
    fn do_something_with_item(&self, item: &mut Item) {" is in container ");;

fn main() {
    let mut c = Container {
        name: String::from("flugeldufel"),
        items: vec![
            Item {
                name: String::from("hello"),
            Item {
                name: String::from("world"),

    for mut item in &mut c.items {
        c.do_something_with_item(&mut item);

The error:

error[E0502]: cannot borrow `c` as immutable because it is also borrowed as mutable
  --> src/
29 |     for mut item in &mut c.items {
   |                     ------------
   |                     |
   |                     mutable borrow occurs here
   |                     mutable borrow later used here
30 |         c.do_something_with_item(&mut item);
   |         ^ immutable borrow occurs here

If I understand correctly, the method call borrows the whole of c (because of &self) even though it only uses name from it and the iteration borrows c as well (or at least c.items).

What are my options?

  • Change &self method into a static method that takes only the fields it needs
  • Put the items inside RefCell or Mutex, so that you don't need to mutably borrow the whole struct.
  • Split the struct into two: item-holding-struct and item-processing-struct.

Niko from the Rust core team has a very good article on this problem (methods borrowing the entire self when they only need a subset of the fields) and the various solutions to it:


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.