In C/C++, returning a pointer to a local variable that gets destroyed when the function exits creates a dangerous dangling pointer. Rust's compiler completely forbids returning a reference to data that goes out of scope—forcing you to return the owned String instead.
ADVERTISEMENT