Re:Comparative unsafety
by OskarS on Friday April 09, 2021 @10:53AM
(#26751287)
I mean, yeah, at the end you only get a raw pointer, the object managing the pointer doesn't exist anymore. I'm not a Rust programmer, but I would imagine it's equivalent to the following C++ code
This creates a std::string, calls c_str() on it to get the pointer. However, since the std::string isn't stored anywhere, its lifetime stops and it is destructed (this is why these are called temporaries), and the pointer is now invalid. The right way of doing it is this:
Now, str and ptr both live in the same scope, and ptr's lifetime is entirely contained in str's.
[ Parent
| on HN ]
There are no comments on this comment yet.