But it seems that flat_map returns a reference to the tuples created, instead passing the ownership to the new function
value of type `Vec<(MyType, bool)>` cannot be built from `Iterator<Item=&(MyType, bool)>
If i then try to add a .map(|&t| t) before .collect() the compiler complains that
cannot return value referencing temporary value
returns a value referencing data owned by the current functionrustc(E0515)
printer.rs(226, 17): returns a value referencing data owned by the current function
printer.rs(226, 17): temporary value created here
I have tried searching on the various errors, but cant seem to find the right solution. So if anyone could point me in the right direction, I would greatly appriciate it!
You've stumbled on the issue of arrays not actually having a true consuming into_iter(). The […].into_iter() actually calls this impl – IntoIterator for &'a [T; N] – and thus is actually equivalent to iter().
There are several possible fixes:
if your type is Copy, add copied() – […].iter().copied().
you can use vec instead of array – vec![…].into_iter()
Yeah, Rust has both, but arrays are a little bit of second-class citizen (like this missing into_iter(), or missing some traits, etc.), so usually usage of Vec is recommended.
Keep in mind that vec implies an allocation, so you should be careful with "just use vec" for perf-sensitive code. (in your particular case, if it's perf-sensitive, I'd go with copied()/map(|&x|x)).