fn main() {
let s = String::new().as_str();
println!("{}", s);
}
I had a problem with my program above:
temporary value is freed at the end of this statement
This happens when I create a String
but only need its reference. I understand the compiler error and the suggested fix. But creating a temporary binding by hand is ugly. I am asking here to see whether Rust has language support to extend the lifetime of an owned value whose borrowed reference is still kept in use, without programmer writing a temporary binding by hand.
I was imagining something like this:
let s = (String::new())@.as_str();
The @
symbol creates an anonymous binding as if the programmer creates it by hand. It is anonymous because it is not being used directly and so doesn't have to pollute the current namespace. This isn't valid Rust yet, but I'd like to know if I can do the same using what Rust already provides, for example, macros.