This fragment does not compile: let first = &v[0]; v.push(4); println!("{}", first);. The error is E0502: cannot borrow v as mutable because it is also borrowed as immutable. In C++ the equivalent code with a std::vector compiles without complaint.
I know the rule about one mutable reference or many shared ones, but I do not see what could actually go wrong here, since push only appends at the end. What is the compiler protecting me from, and what are the usual ways to restructure such code?




