A reference is only valid while the value it points to still exists. Rust prevents us from returning a reference to a local value that is about to be destroyed in source. The function will not compile:
fn create_message() -> &String {
let message = String::from("Server started");
&message
}message is dropped when the function ends. Returning a reference to it would leave the caller with a reference pointing to invalid memory
Transclude of rust-prevents-dangling-reference
Returning the owned value fixes the problem:
fn create_message() -> String {
String::from("Server started")
}Ownership goes back to the caller so the value stays alive