0

I have a slice of bytes start = [30u8; 5] and middle = [40u8; 3] and a vector of byte slices:

let first = [1u8; 10];
let second = [2u8; 10];
let third = [3u8; 10];

let elements: Vec<[u8; 10]> = vec![first, second, third]; 

I want to concatenate everything together, in such a way that I will obtain a single byte slice which looks as

[30, 30, 30, 30, 30, 40, 40, 40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]

However, although I can concatenate start and middle when I try to append the vector elements it fails. I know that I am wrongly trying to iterate through the elements of the vector to concatenate, but I can't figure out how to do it correctly?

fn main() {
    let start = [30u8; 5];
    let middle = [40u8; 4];
    let first = [1u8; 10];
    let second = [2u8; 10];
    let third = [3u8; 10];

    let elements: Vec<[u8; 10]> = vec![first, second, third];
    println!("{:?}", elements.iter());

    for key in elements.iter() {
        println!("{:?}", key.iter());
    }

    let alltogether: Vec<u8> = start
        .iter()
        .cloned()
        .chain(middle.iter().cloned())
        .chain(elements.iter().iter().cloned())
        .collect();

    println!("{:?}", alltogether);
}

This example can be copy-pasted into the Rust playground.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Ziva
  • 3,181
  • 15
  • 48
  • 80
  • Your question might be answered by the answers of [How do I concatenate two slices in Rust?](https://stackoverflow.com/q/40154150/155423); [Concatenate array slices](https://stackoverflow.com/q/37585808/155423); [How can I concatenate two slices or two vectors and still have access to the original values?](https://stackoverflow.com/q/54142528/155423); [How to concatenate static arrays in rust?](https://stackoverflow.com/q/55893216/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Apr 08 '20 at 15:04
  • [Whats the best way to join many vectors into a new vector?](https://stackoverflow.com/q/39263555/155423) – Shepmaster Apr 08 '20 at 15:04
  • 1
    `.chain(elements.iter().flatten().copied())` ? – Shepmaster Apr 08 '20 at 15:10
  • Does this answer your question? [How do I concatenate two slices in Rust?](https://stackoverflow.com/questions/40154150/how-do-i-concatenate-two-slices-in-rust) – E_net4 Apr 08 '20 at 15:10
  • I feel [Whats the best way to join many vectors into a new vector?](https://stackoverflow.com/questions/39263555/whats-the-best-way-to-join-many-vectors-into-a-new-vector) is a better match. `[first, second, third].concat()` works for the first example – trent Apr 08 '20 at 15:36
  • The only answer which worked was the one by @Shepmaster with the function `flatten`, since I want to concatenate `start`, `middle` and `elements` in one line, without the need to take separate the items within the vector `elements`. The other answers seem to apply to a scenario in which I want to concatenate `start, middle, first, second, third` when they are given as independent slices. – Ziva Apr 08 '20 at 15:45

1 Answers1

1

You possibly want this:

    let alltogether: Vec<u8> = start
        .iter()
        .cloned()
        .chain(middle.iter().cloned())
        .chain(elements.iter().flatten().cloned())
        .collect();

Note that there is also copied (instead of cloned) that can be used for Copyable types.

If the stuff in elements does not implement IntoIterator itself, you can use flat_map to specify how to convert one element to an iterator.

phimuemue
  • 34,669
  • 9
  • 84
  • 115
  • Yes, thank you, this is exactly what I was lookign for! Also the advice about `flat_map` is gold! – Ziva Apr 08 '20 at 16:29
  • You could also try something like `[start, middle].chain(elements).flatten().cloned()`, but I did not get this one up and running. – phimuemue Apr 08 '20 at 16:38