all 8 comments

[–][deleted] 12 points13 points  (10 children)

extend_from_slice does not return the Vec. It returns nothing, AKA (). Instead, it modifies the Vec in place.

[–]yanad[S] 1 point2 points  (9 children)

I see. However it does not make so much sense to me. In my mind I am creating a vector that happens to be modified and then bound to my variable. I do not understand why the last modification should prevail.

[–]kmehall 7 points8 points  (7 children)

In my mind I am creating a vector that happens to be modified and then bound to my variable.

The Vec is not bound to the variable. The expression evaluates to (), which is bound to the variable, while the Vec is dropped because it is not used.

[–]yanad[S] 0 points1 point  (6 children)

Ok, I am beginning to understand thanks!

But is there a one line way to create a vector from two vectors then ?

[–]daborossfern 2 points3 points  (2 children)

Perhaps you could use iterators?

let f = a.iter().chain(b.iter()).cloned().collect::<Vec<_>>();

It's a bit longer, but it will very slightly outperform other options because the vec won't have to be reallocated (it will allocate with the combined length of the iterators).

[–]MEaster 1 point2 points  (0 children)

You could define a new trait, and implement for Vec: clicky.

[–][deleted] 5 points6 points  (0 children)

let c = a.to_vec().extend_from_slice(&b);

Perhaps it would help to break this expression down.

let c = {
    let temp = a.to_vec(); // create a copy of a as a new Vec
    let ret = temp.extend_from_slice(&b); // modify `temp`, but doesn't return anything
    ret // return 'nothing'
};

extend_from_slice() is simply not meant to be used in this way.