Fix Error E0515 by returning an owned value (like a String) instead of a reference to a local variable that will be dropped. Change the function signature to return String and clone or create the data inside the function so it lives on the heap.
fn get_greeting() -> String {
let message = String::from("Hello");
message // Returns ownership of the String
}
fn main() {
let s = get_greeting();
println!("{s}");
}
If you must return a reference, the data must outlive the function (e.g., passed as an argument or static).
fn get_greeting<'a>(s: &'a str) -> &'a str {
s // Returns a reference to input data
}
fn main() {
let input = String::from("Hello");
let s = get_greeting(&input);
println!("{s}");
}