Question: I have a fixed size `u8` array of size `2048`, which gets filled from a network data of varying lengths. I need to copy this data to a `vec` of `u8` of size equal to received length.

This is how I used to do in c++

```
char buff[2048];
ssize_t data_len = recvfrom(socket, buff, sizeof(buff), 0, nullptr, nullptr);

std::vector<char> vec_buff(buff, buff + data_len)

```

I know `Vec<T>` impls `From<[T; N]>` and it can be created from an array by using the `From::from()` method, but this takes the entire size of `2048` but I want only `data_len` bytes.

Answer: 
A simple

```rust
let vec = buff[..data_len].to_vec();

```

will do if your type is `Clone`.
`buff[..data_len]` takes a slice of the first `data_len` elements, `to_vec` then turns that slice into a `Vec`

If it isn't you can use a variation of @Emoun s answer:

```rust
let vec = buff.into_iter().take(data_len).collect::<Vec<_>>();

```
You could use [`Iterator::take`](https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.take):

```rust
let array: [u8;2048] = ..;
let data_vec: Vec<_> = array.iter().cloned().take(data_len).collect();

```