| > Do you really want an error returned from push_back? For most people, no, you definitely want it to just work or explode, which is indeed what happens in normal Rust, and, not coincidentally, the actual effect when this exception happens in your typical C++ application after it is done with all the unwinding and discovers there is no handler (or that the handler was never tested and doesn't actually somehow cope). But, sometimes that is what you wanted, and Linus has been very clear it's what he wants in the kernel he created. For such purposes Rust has Vec::try_reserve() and Vec::push_within_capacity() which let us express the idea that we'd like more room and to know if that wasn't possible, and also if there was no room left for the thing we pushed we want back the thing we were trying to push - which otherwise we don't have any more. There is no analogous C++ API, std::vector just throws an exception and good luck to you AFAIK. |
https://godbolt.org/z/6xE6jr3zr ?