3 ms·
About 11 minutes in as an aside they look at an example where they're initially mutating a growable array (Rust's Vec, Mojo's List) and now they want to replace
by tialaramex 3y ago
About 11 minutes in as an aside they look at an example where they're initially mutating a growable array (Rust's Vec, Mojo's List) and now they want to replace it instead.
The narrator (rather than Chris) shows that Mojo makes this fairly easy - we were never promising only to mutate the container, so if we replace it instead that's fine. And they (presumably correctly, I am not a Python expert) say that Python just can't do this.
But they also claim Rust can't do it either, and propose a fairly serious surgery to adjust the function signature instead to write a function which takes and returns ownership.
But you don't need to do that in Rust. We have a mutable reference, Rust doesn't mind us changing what it refers to, it's mutable after all, we just can't move it because it's not ours to move. This is where core::mem::swap, core::mem::take and core::mem::replace enter the picture, they allow us to easily use this capability.
For the example they're explaining you can just core::mem::replace(v, vec![8, 8]); and you're done, the signature doesn't change at all.