From this, I filter the array with the Player ID, and this can net to a Vec of length 0 or 1.
I then want to grab all the strings from it, and return Vec
mii_names
.clone()
.into_iter()
.filter(|r| r.player_id == chadsoft_id_data.player_id) // Result can be an empty vector
.map(|r| {
r.mii_names
.into_iter()
.map(|r| r.mii_name)
.collect::<Vec<String>>()
})
.collect::<Vec<Vec<String>>>()
.get(0)
.unwrap_or(&vec![])
.to_vec()
This is the best solution I could come up with, but it feels over the top.
Note that you can skip many allocations by not cloning whole Vec<MiiNames>, but instead using Iterator::cloned adaptor, or just cloning mii_name. For example:
You can save even more memory, if you use Arc<str> instead of String for representing mii_name. For detailed explanation I recommend Use Arc instead of Vec video by Logan Smith.
I knew of Arc but in the whole codebase I have to use it as Vec due to other reasons, also this whole ordeal is going inside of yet another Struct - though I did not know about .cloned(), thanks!
Edit: I actually cannot clone just the strings themselves nor use Iterator::cloned because this whole thing is within a loop, so mii_names would get moved into without the initial call to clone, though that is beyond the scope of this thread.