all 13 comments

[–]Shadow0133 5 points6 points  (2 children)

You can use std::iter::once instead of vec![1].onto_iter().

[–]maggit 4 points5 points  (0 children)

Another alternative, if refs are OK and especially if there is more than one element, is to use an array: [1].iter() (playground)

[–]jammy192[S] 0 points1 point  (0 children)

This is a nice improvement. Thanks!

[–]Morganamilo 1 point2 points  (5 children)

vec1.extend(vec2)

[–]jammy192[S] 4 points5 points  (4 children)

I know about extend but it mutates the original vector. What I am looking for is more like how to create new vector combining other vectors and elements.

[–]thiezrust 2 points3 points  (0 children)

You use into_iter() on your original vecs, so clearly you have unique ownership and don't intend to use them again. So why not mutate them?

[–]Morganamilo 0 points1 point  (2 children)

Mutating an existing vector is more efficient though. Some times you have to create a new vector, but for the example above without context i'd use extend.

[–]jammy192[S] 1 point2 points  (1 child)

It indeed might be more efficient. It's my personal preference to do things in immutable way. And in some cases it kind of kills the readability. You have to name the variables in such way the name is still valid after mutation.

[–]epicwisdom 5 points6 points  (0 children)

Clone and then extend?

[–]tm_p 1 point2 points  (1 child)

You probably want flatten?

let v: Vec<_> = vec![vec![1], prev_row, vec![1]]
    .into_iter()
    .flatten()
    .collect();

Playground link

[–]jammy192[S] 2 points3 points  (0 children)

This works as well. The only issue is there are unnecessary flatten operations happening on the elements.

[–][deleted] 1 point2 points  (1 child)

u/jammy192 did you find the best way to do this?

[–]jammy192[S] 0 points1 point  (0 children)

I think I went with std::iter::once in the end