Problem using closures, single threaded: move occurs because `X` has type `std::string::String`, which does not implement the `Copy` trait

Playground

I'd like to use closures to make things a little more manageable.

What is my problem here?

into_bytes consumes the String to give you back a Vec, since you only have a reference you can't consume it, but as_bytes is here for you.

You will get another error because you take &Vec<u8> as argument (which you should never do) but you can fix it by using &[u8].

2 Likes

Out of curiosity, why should you never do that?

&Vec<T> doesn't do anything useful that &[T] can't do and using a slice allows to accept other types.
This stack overflow answer has an in-depth explanation.

3 Likes