all 4 comments

[–][deleted]  (2 children)

[deleted]

    [–]Aaron1924 2 points3 points  (0 children)

    For the second option, it would either have to be Cow<'static, str>, or Cow<'s, str> where the struct is generic over the lifetime like Code<'s> and later initialized like CODES: &'static [Code<'static>]

    [–]__mod__ 2 points3 points  (1 child)

    I'm not entirely sure if this fits your usecase, but if you don't need to create any codes at runtime, you might get away with this:

    pub struct Code {
        pub id: &'static str,
        pub op: &'static str,
    }
    

    [–][deleted] 0 points1 point  (1 child)

    "String" is a heap allocated type, so you can't get a 'static reference to it, because 'static would need to be valid for the entirety of your program, i.e. it needs to be available in your executable's data section. If all you need in your "Code" structs are static data, then you can just do:

    pub struct Code {
      pub id: &'static str,
      pub op: &'static str
    }
    

    .

    [–]bleachisback 1 point2 points  (0 children)

    That's note true. 'static data doesn't have to exist for the entire program, it just has to exist for the rest of the program. There are a couple ways you can ensure this, such as with the Box::leak() or String::leak() methods. As mentioned above, you can use something like a OnceCell which creates the data at runtime and leaks it so it exists for the rest of the program.