Here is my code:
fn find_rects<'a >(
rects: & Vec<&'a Rectangle>, // reference of a vec containing Rectangle references
width_limit: u32)
-> Vec<&'a Rectangle> // returns a vec of Rectangle references
{
rects
.iter()
.filter(|x| x.width > width_limit)
.collect()
}
It failed to compile. The error message says:
.collect()
^^^^^^^ value of type `Vec<&Rectangle>` cannot be built from `std::iter::Iterator<Item=&&Rectangle>`
And I found an answer to use .copied().collect()
instead of .collect()
. I tested and it works.
But I don't know the reason.