3 ms·
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
by OskarS 6y ago
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
auto ptr = std::string(something).c_str();
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:
std::string str { something };
auto ptr = str.c_str();
Now, str and ptr both live in the same scope, and ptr's lifetime is entirely contained in str's.