Borrowed value does not live long enough for assignment

This is because you are trying to return a reference to something on the stack. This reference will get invalidated on return. Moreso, you are creating a reference to something that will be destroyed at the end of each loop.

fn placeSuits(hand: [u32; 5], mut newHand: [&'static str; 5]) -> [&'static str; 5] {
    for i in 0..4 {
        let s: String = hand[i].to_string(); // create a new owned string
        let ss: &str = &s; // reference that owned string
        let ss = &s[..];  // still a reference to the owned string s
        newHand[i] = ss; // tries to unify the lifetime 'static with the lifetime of s

        // s is dropped (destroyed) here, so it can't be unified with 'static
    }
    newHand
}

Instead of calling str::to_string, you can just copy the reference to the string and utilize that.

fn placeSuits(hand: [u32; 5], mut newHand: [&'static str; 5]) -> [&'static str; 5] {
    for i in 0..4 {
        newHand[i] = &hand[i];
    }
    newHand
}

This is fine because you are just creating a reference to the referent of another reference. This is basically just copying the reference. So the lifetimes will also be copied over, and everything will work out.


if you want to read more about the differences between str and String