I need to prepend a number elements (prefix) to an existing slice.
My idea was to use iterators for this. Specifically, iter::repeat() and take() to generate the prefix of required length, followed by chain() to append the existing data (slice), and finally collect() to dump the combined data it into a Vec<T>. But I can't get it to work:
are both possible solutions to copy the bytes from data into the new vector (using copied is more readable than the implicit copy that happens when we return the dereferenced byte from the closure we pass to map in the prepend2 IMO). Playground.
If there is a lifetime involved when you don't expect one, you can bet something is a reference when you think it's a value. The compiler mentions the item type explicitly, which you can then look up in the documentation.