I have an enum MyEnum
with as_str
method implemented.
What I want to do is to make it so that I could return &str
formatted with SPACING
from match
arm.
For example instead of "ONE"
I would get format!("{SPACING}ONE{SPACING}")
, but the problem is format
macro returns owned string and the attempt to return a reference results in cannot return reference to temporary value
error.
enum MyEnum {
One,
Two,
Three,
}
const SPACING: &'static str = " ";
impl MyEnum {
fn as_str(&self) -> &str {
match self {
MyEnum::One => "ONE",
MyEnum::Two => "TWO",
MyEnum::Three => "THREE",
}
}
}
So I have two questions:
- Is there a way to format string literal and return a string slice?
- Also I'm a bit confused about what owns string literal inside
match
arms, I guess it'sstatic
and exists throughout the entire program execution time or is it wrong?