Vec can not be built from iterator

Playground link

In the code, I'm trying to turn a number like 101 into 111000111. I first have some code to split up the number and triple the digit. However, when I tried to iterate over vector composed of each number ([1,0,1]), I get this error:

value of type `&mut Vec<u32>` cannot be built from `std::iter::Iterator<Item=u32>

This is confusing, because this line previously worked - .collect() ran with zero issues, and the assert_eq! macro provides no panic or error messages. What am I missing? Is the loop doing something weird to my vector?

Your playground link doesn't reproduce the error, so I can't use it to understand the issue.

Look closely at the type there. Apparently youre trying to make a &mut Vec<_>, not a Vec<_>.

Sorry, here's a working playground link

You're calling append, which takes &mut Vec<_>: Vec in std::vec - Rust

What you want instead is extend: Vec in std::vec - Rust

And as a bonus, that means you won't even need the collect.

1 Like

Thanks! More reading for me to do. I remember a quote I once read when I first started coding. It was something along the lines of: "Coders don't spend their time writing code - they spend their time reading it." Truer words...

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.