I am trying to store an object into 2 different reference vectors, and if I modify the object from first vector ref, it should be visible from the second vector.
I still haven't understood well borrowing so that's what I'm trying to acheive :
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result;
use std::vec::Vec;
struct A {
x: u32,
}
impl Display for A {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "{}", self.x)
}
}
fn main() {
let mut a = A{x: 1};
let mut v1: Vec<&mut A> = Vec::new();
let mut v2: Vec<&mut A> = Vec::new();
v1.push(&mut a);
v2.push(&mut a);
let f: &mut A = v1[0];
f.x = 3;
for v in &v1 {
println!("Value: {}", v);
}
for v in &v2 {
println!("Value: {}", v);
}
}
Of course this doesn't compile. What should I do to be able to store same object in different collection like objects ? (I don't see any concurency issue or borrowing here).