External Publication
Visit Post

Interior mutability and safety of ownership transfer in Rust

Rust Internals [Unofficial] March 5, 2026
Source

Eh2406:

If "non-owning" includes &mut Vec<T>, then no. What if the Vec<T> is at capacity and both copies call push? Then you get a double free . More generally &mut means exclusive, so &mut v1[0] and &mut v2[0] is UB on its own.

If by "non-owning" you mean &Vec<T>, you will need better languish lawyers then me.

No, this is not what I mean. In my case there are two bitwise-equal copies of the same Vec (pointer, len and capacity are all equal), but one is essentially a "non-owning" ManuallyDrop<Box<Vec<T>>> and the other in an "owning" Box<Vec<T>>. This is quite typical in unsafe low-level data structures dealing with manually allocated memory*, but it is usually not possible for both instances to be observable externally.

(*) temporarily, inside a single function

Discussion in the ATmosphere

Loading comments...