I have the following code.
fn main() {
fn return_string_view() -> &'static str {
const val : String = std::format!("{value}", value = "some value");
&val
}
}
What I want is have the formatted string precalculated before the runtime. What I have tried is using the const keyword, however in the example above I get error[E0658]: 'match' is not allowed in a 'const'
. I need to understand where this error comes from. Previously I used something like this :
fn return_string_view() -> &'static str { "some value" }
but now I want this expression to be composed from several values, just to refactor code, like here:
fn first() -> '&static str { "using {}", return_string_view() }
fn second() -> '&static str { "something else using {}, return_string_view()}
Of course, this would not compile, as I need to use format to format strings. So I wrote this
std::format!("{value}", value = "some value");
, which returns a String
, but I prefer to use &str
in my code, because there is really no sense in creating these Strings, which clearly can be formed before a program even launches.
I need to tell Rust that the string that I am trying to create is something that can be hardcoded into the program image, it never changes, so, to me, const
fits this purpose, but is what I am trying to do possible?
Please tell me why it is not possible.