Back to questions
JuniorTheoryCommon
What does the program print?
fn main() {
let v: Vec<i32> = (1..=5).filter(|n| n % 2 == 1).map(|n| n * n).collect();
println!("{v:?}");
}[1, 9, 25]
- Expecting
[4, 16], as iffilterthrew away the numbers its closure accepts: it keeps them, so the odd 1, 3 and 5 go on tomap. - Expecting
[1, 9], as if1..=5stopped before 5: the=makes the range include its end, so 25 is in the result too.
- What does the chain produce if you swap
filterandmap? - Why does
collectneed the typeVec<i32>written onv?