I've the below function, it works correctly if I want to return x or y, but it fail to return x+y
fn test() -> &'static str {
let x = r#"
Hi there
"#;
let y = r#"
Hi man
"#;
let mix = format!("{0}{1}",x,y);
&mix
}
It gave me the below error:
error[E0515]: cannot return reference to local variable `mix`
--> src/main.rs:26:5
|
26 | &mix
| ^^^^ returns a reference to data owned by the current function
Concatenating two strings together like you did requires the function to create a new buffer. That buffer has to be owned on the stack somewhere. If it can't just pass ownership up to its caller, then it will have to put the buffer somewhere else or get it from somewhere (like a parameter, though this function doesn't accept any, or a global or thread-local storage).
I'm not sure how the 3rd party module requires this. Do you need to pass test() to another function? If, for example, the third party module has a function similar to
&'static str basically means only string literals are allowed, and any dynamically created strings are not supported. So if a third party module has such requirement, then the third party is forbidding you from using format!.
If you must, then Box::leak(format!("foo").into_boxed_str()) will transform String into &'static str, but don't do it. That's a hack. The program will never free that memory.
Returning &str is very limiting for functions. Error::description() in Rust's stdlib made that mistake, which made the whole function useless, and it got removed as a result.
The function should return String to allow dynamically created strings. It may return Cow<'static, str> to allow either &'static str or String without having to clone the str.