this post was submitted on 17 May 2025
29 points (93.9% liked)
Rust
7088 readers
21 users here now
Welcome to the Rust community! This is a place to discuss about the Rust programming language.
Wormhole
Credits
- The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
If you want guaranteed safety then the borrowing rules are the most flexible as far as we know.
Just to give a couple of examples of how your idea might be flawed, what do you consider "basic types"? Are enums basic types? Then you've got an issue, because you might get a reference to the contents of an enum and then replace the enum with another variant, and suddently you've got a dangling reference. Would you prefer to prevent creating references to the contents of an enum? Then you're more restricting than the borrowing rules.
Allowing iterable types but not iterating over mutable references is not enough for safety unfortunately. The basic example is getting a reference to an element of a
Vec
and then callingpush
on theVec
. This seems very innocent, butpush
ing on aVec
might reallocate its backing buffer, making the previous reference dangling. Again, would you prevent taking references to elements of aVec
? Then again you become much more restricting than the borrowing rules.That was just syntax sugar for
Rc
/Arc
, and you can still use them in today's Rust, albeit with slightly worse ergonomics (no autoclone for example).