3 ms·
>Wouldn't this work? https://doc.rust-lang.org/std/mem/fn.swap.html https://doc.rust-lang.org/std/mem/fn.swap.html If you click the [src] link on that page, yo
by commit 7y ago
>Wouldn't this work? https://doc.rust-lang.org/std/mem/fn.swap.html https://doc.rust-lang.org/std/mem/fn.swap.html
If you click the [src] link on that page, you'll see that the implementation of std::mem::swap uses unsafe. I'm no Rust expert, but I'm also not aware of a way to swap pointers without unsafe.
- carlmr 7y agoAh I thought you meant without using unsafe yourself. The goal of unsafe is to reduce the attack surface where you need to check your code more thoroughly. So by encapsulating this in a library function that's well tested you don't need to create such a section yourself. But if you do need unsafe it's not wrong, and if only a small part of your code needs unsafe then it's much easier to verify that your code is doing what it should.