3 ms·
(er, some text in the parent seems to have vanished) Sure, if you absolutely wish it to, in an `unsafe` block, you can define two writeable pointers to the sam
by Yoric 5y ago
(er, some text in the parent seems to have vanished)
Sure, if you absolutely wish it to, in an `unsafe` block, you can define two writeable pointers to the same piece of data. I don't think I've ever seen anybody write such code in Rust (except perhaps when linking to a C library that required it?), nor have I ever felt the need to do so.
Now, it is true that Rust is designed to discourage writing cyclic data structures. I can imagine that trying to port (rather than redesign) an algorithm that deeply requires cyclic data structures, that will make your life miserable.
- wizzwizz4 5y agoYou actually can't define two `&mut` references to the same data in Rust. It's invalid Rust code; if the compiler notices, it'll refuse to compile, and if it doesn't notice, your code will randomly break.
- Yoric 5y agoYou can define two `*mut`, though, in unsafe code.
- anderskaseorg 5y agoA better option is to use Cell or RefCell; they allow completely safe shared mutability in single-threaded code. https://doc.rust-lang.org/std/cell/ https://doc.rust-lang.org/std/cell/
- Yoric 5y agoI'm aware of that :) I'm just trying to decypher that cryptic sentence from the OP.
- pwdisswordfish9 5y agoYou can, just not at the same time.