0

I want to create an array called minutes: [&'static str; 60] that contains the "stringified" values from 1 to 59. So far I got:

pub const fn minutes_as_str() -> [&'static str; 60] {
    (1 ..= 59).map(|c| c.to_string()).collect()
}

However the collect() won't work with such iterator. I tried using a for loop too but the BC gets in the way:

pub const fn minutes_as_str() -> [&'static str; 60] {
    let mut result = [""; 60];
    for n in 1 ..= 59 {
        result[n] = &n.to_string(); // BC failure
    }
    result
}
noconst
  • 639
  • 4
  • 15
  • [This question and its associated answer](https://stackoverflow.com/questions/57225055/in-rust-can-you-own-a-string-literal) should explain why it is not possible to do so. `&str`, even if you define its lifetime as `'static`, is still a reference to something. Where do you think the data is stored? – Sébastien Renauld Sep 25 '19 at 22:08
  • Possible duplicate of [How can I create 100 distinct labels with type &'static str?](https://stackoverflow.com/questions/47803862/how-can-i-create-100-distinct-labels-with-type-static-str). I would recommend code generation if 60 is a compile time constant. `["1", "2", "3", ... "59"]` might not be *beautiful*, but it's trivial to validate and (presumably) never needs to change. – trent Sep 25 '19 at 22:57
  • 2
    (Note that `1..=59` is only 59 elements, not 60; you appear to have a fencepost error.) – trent Sep 25 '19 at 23:02

0 Answers0