Naveen Santhanavel
Naveen Santhanavel

Reputation: 439

Cannot hold immutable and mutable references to different fields of a struct which is inside a Vec

Holding an immutable reference to one field in a struct while modifying another field is fine. However if the said struct is inside a Vec, it is not possible. What is an alternative data structure to make this work?

struct Data(String,i32);
fn main(){
    let mut v = vec![Data("id".to_string(), 0)];  // Data instance owned by Vec
    let reference_to_id = &v[0].0 as &str;        // Reference to 1st field of Data to be stored elsewhere
    v[0].1 = 1000;                                // Modifying 2nd field of Data

    println!("{}", reference_to_id);              // ERROR
}


error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
 --> r4.rs:5:5
  |
4 |     let reference_to_id = &v[0].0 as &str;
  |                            - immutable borrow occurs here
5 |     v[0].1 = 1000;
  |     ^ mutable borrow occurs here
6 |
7 |     println!("{}", reference_to_id);
  |                    --------------- immutable borrow later used here

I want to store a bunch of struct instances in a Vec, the "id" fields of these structs wouldn't change and I want to use references (&str) to this field in other parts of the code. Does this mean, I cannot modify any field in these structs? This feels very restrictive.

Upvotes: 0

Views: 92

Answers (2)

Frost.
Frost.

Reputation: 144

This should work.

struct Data(String,i32);
fn main(){
    let mut v = vec![Data("id".to_string(), 0)];  
    let item = &mut v[0];

    item.1 = 1000;
    println!("{}", item.0);            
}

Upvotes: 1

Chayim Friedman
Chayim Friedman

Reputation: 70830

The borrow checker does not understand vectors. But you can retrieve a reference to that element, and then it'll work:

let r = &mut v[0];
let reference_to_id = &r.0 as &str;
r.1 = 1000;

Upvotes: 3

Related Questions