Why does static promotion work here?

fn f() -> &'static str {
    let a = rand::random::<u8>() % 4;
    &"1234"[a as usize..]
}

playground

I learn the trick from fn_name crate.

There is no static promotion happening here at all. The string literal was &'static str all along. The indexing operation is fn(&'a str, RangeFrom<usize>) -> &'a str and thus preserves the 'static lifetime.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.