You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Wrong translation for iterator into moved container #578
std::vector<int> src{1, 2, 3};
std::vector<int> dst;
auto it = src.begin(); // (1) iterator to src's first element
dst = std::move(src); // (2) dst steals src's heap buffer; src is left emptyassert(&*it == &dst[0]); // (3) C++: true, the element never moved, dst owns it now
let src:Value<Vec<i32>> = Rc::new(RefCell::new(vec![1,2,3]));// Value<T> = Rc<RefCell<T>>let dst:Value<Vec<i32>> = Rc::new(RefCell::new(Vec::new()));let it = src.as_pointer().begin();// (1) Ptr = (Weak to src's Rc, index 0)
dst.write(std::mem::take(&mut*src.borrow_mut()));// (2) the Vec moves into dst's Rc; src's Rc keeps an empty Vec;// `it` still refers to src's Rcassert!(it.deref() == dst[0]);// (3) panics: index 0 out of bounds of the empty Vec in src
In C++ the iterator for vector (or other sequential container) stays valid after src is moved because it points into the same buffer. In Rust the iterator is a Weak reference into the src Value<Vec<T>>. It still points into src after the move.
I think the correct solution is to translate vector as Value<Value<Vec<T>> but this is too costly. Also I am not sure if the behavior in C++ is standard behavior or implementation behavior. I found no explicit sentence in the standard that enforces this.
Activity
lucic71 commentedon Nov 20, 2026
Same for
std::swap, iterators into the container remain valid.