Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f...
by blegr on Sunday May 05, 2024 @12:59PM
(#40266318)
Now that GP said it, it'd be nice to be able to write "some_map[i].value_or(some_value)".
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f...
by umanwizard on Sunday May 05, 2024 @01:21PM
(#40266499)
You can do this in rust with Vec::get, which returns an Option<&T>.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f...
by umanwizard on Sunday May 05, 2024 @01:20PM
(#40266494)
No, that’s my point. It returns a reference. So it’s a good example of when you might want to return a reference, which you said seemed uncommon. But the reference it returns is unsafe (for example, it gets invalidated if the vector is later resized), whereas the reference returned by the corresponding Rust operator is safe.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f...
by frutiger on Sunday May 05, 2024 @01:34PM
(#40266598)
Sure, but the person I was relying to said they were writing code that returns references (not implementing the standard library). And my surprise was that they have to do it so often.
My overall advice here would be that if you find yourself implementing vector-like data structures very often, then it is time to take a second look the design.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f...
by umanwizard on Sunday May 05, 2024 @01:55PM
(#40266771)
Fair enough, but even if you’re not writing code like this, you still have to use it, so you’re still exposed to the unsafety.
Are you asking in a theoretical world where it isn’t defined to already return a `T&`?