Populating a vector with vectors AND vectors with references

I’m having trouble trying to figure out how can I dynamically populate some sort of vector with structures of the following type, inside a loop/iterator/etc.:

struct SplitData<‘a> {
  data: Vec<u8>,
  split_1: Vec<&‘a u8>,
  split_2: Vec<&‘a u8>,
}

In the structure above, split_1 and split_2 themselves will be vectors of references to elements already inside (and owned by?) the data vector.

The main issue I keep facing is the infamous error “ borrowed value does not live long enough” because the data vectors are created inside the loop.

Could anyone shed some light into the right pattern I should implement to do this?

This is not possible in Rust. A struct can never reference anything owned inside itself.

Search for "self-referential struct" for discussions and workarounds.

2 Likes

Much appreciated, will read those posts and see what I learn and come up with :stuck_out_tongue:

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.