Sometimes being nice really pays off.
std::optional<> is an extremely useful and welcome addition to C++ that improves code quality, is easy to understand, and has easy to reason about code generation.
No doubt there is something that could be demonstrated in Rust with Option that is compelling, but I'd rather people showed that so a basis for comparison with modern C++ code can be made.
I suspect in practice these arguments make little difference to real world code.
Here's the big issue: unchecked access to std::optional with operator* has undefined behavior when there's no value.
This is unforgivably bad design since you can enforce exhaustive checking at compile time, but C++ isn't going in that direction.
std::optional offers value() for checked access too, but that checks at runtime and throws an exception.
It is possible to have an optional with no runtime check, no undefined behaviour, and guaranteed handling of both cases by checking at compile time. This is what Rust offers.
Undefined behaviour being easy to invoke absolutely does matter for real world code.
"Make everything constexpr" isn't a real solution to UB, in the same way that "make all functions pure" isn't a solution for managing side effects.
Not adding UB to your APIs, on the other hand, is a real solution.
unsafe fn super_unwrap<T>(x: Option<T>) -> T {
match x {
Some(val) => val,
None => unreachable_unchecked!(),
}
}
But defaults matter, and Rust certainly doesn’t make this kind of thing ergonomic (which is a correct decision on the Rust designers’ part).Because all Rust's methods can be called as free functions, you can literally write Option::unwrap_unchecked for the same behaviour, or you can some_option.unwrap_unchecked() (in both cases you will need to be in unsafe context for this to be allowed and should write a SAFETY comment explaining why you're sure it's correct)
Including UB in easy to misuse places is totally unnecessary and a footgun which really does cause issues in real code.
An equivalent API with no UB is just strictly better.
I have never once missed the distance of an optional<T&>. The practical use of optional<> is that you know where T is going to be constructed and can reason about the memory layout. Using it as an alternative to T* has never even occurred to me. The ideal of bundling a presence flag and a pointer together (which would be the default underlying representation unless it was specialised to hold a T* internally) is gross and inefficient
That's a defect in C++ rather than some principled objection though. Rust's Option isn't specialized, the Guaranteed Niche Optimisation kicks in exactly the same for &T as for most C-style enumerations, OwnedFd, NonZeroU8 or indeed my BalancedI8, this is one of those places where an engineer can see how to design the core language properly to deliver the same performance despite better ergonomics for everybody, not add a special hack.
In practice since C++ can't do that, the likely C++ 26 std::optional<T&> will be a specialization which just has a pointer inside it. This may mean lots of awkward word smithing to require that implementation or they may just trust that all the implementers will Do The Right Thing™, as with the Niebloids.
I try not to "speak with authority" about C++ because I don't think anybody has the necessary understanding to do so, including the people who wrote the ISO document, the compiler engineers, and Bjarne himself.
The practical use for me is making interfaces safer. Where I saw colleagues use pointers as optionals, end up mis-tracking what can be null and what can't, only checking it inconsistently, and triggering UB, I now have a clear distinction between optional and non-optional arguments/returns with an easy way to access the contained .value() without risk of UB. The type also tells me when I should handle the empty case and when I shouldn't.
Most of the time, I want to pass/return a reference and the lack of `optional<&T>` makes it tiring. If only `std::reference_wrapper` had a shorter name, I could at least use that. But then I'd end up with `arg.value().get().attr` when `.value().attr` should be enough...
Surprised to hear that you want to return a reference so frequently.
Me? References get returned all the time because you want to access some state store's vector of things without copying the vector just to ask "are any of the elements X?" or adding a new method for every `std::algorithm` method for each member you might want to use it on.
The benefit of `optional<T&>` over `T*` in an API is that the former communicates "you have write access to this thing which may not exist" whereas the second needs documentation for whether `nullptr` is a thing and whether the caller needs to `delete` it (or was it `delete[]` this time?).
This is an uncharitable characterisation of what I said.
> References get returned all the time because you want to access some state store's vector of things without copying the vector just to ask "are any of the elements X?"
This is what const references are for. Returning an optional<vector<T>&> to query if it contains an element would not be appropriate.
This to me is the clearest example of something that’s safe in Rust, and impossible to make safe in C++.
Are you asking in a theoretical world where it isn’t defined to already return a `T&`?
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.