A guy on discord got pretty confused by this.
Could you please give a little more information here? What should the Lint warn about or suggest? Maybe a code example would also be helpful.
The vec! macro uses Clone, and since Rc's clone doesn't duplicate the underlying data, all the elements of the Vec end up pointing to the same thing. This can cause surprising behavior with interior mutability.
use std::{rc::Rc, cell::Cell};
let v = vec![Rc::new(Cell::new(false)); 3];
v[1].set(true);
println!("{:?}", v); // [true, true, true]
I'm not sure about the _best_ alternative, resize_with seems like a good option but is currently unstable. Possibly something like,
(0..N).map(|_| Rc::new(..)).collect()
¯\_(ツ)_/¯
Most helpful comment
The
vec!macro uses Clone, and since Rc's clone doesn't duplicate the underlying data, all the elements of the Vec end up pointing to the same thing. This can cause surprising behavior with interior mutability.