In rust, how to create a string slice &str
, from a arithmetic expression, say 1 * 2 * 3
, in one line?
i.e. the following code:
fn main() {
let a = vec!["x", &(1 * 2 * 3).to_string()];
println!("{:?}", a)
}
produces the following error:
error[E0716]: temporary value dropped while borrowed
--> src/main.rs:4:24
|
4 | let a = vec!["x", &(1 * 2 * 3).to_string()];
| ^^^^^^^^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary which is freed while still in use
5 | println!("{:?}", &a);
| -- borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
But doing as the compiler suggests would require an extra statement:
let num = (1 * 2 * 3).to_string();
let a = vec!["x", &num];
Is there a way to get rid of the let num = …
statement?