Curriculum Collections & iteration Closures
Closures
Rust closures look like compact TypeScript arrow functions, but they capture by borrow, mutable borrow, or move depending on use. Planned exercises: 1. choose closure syntax. 2. recognise inferred parameter types. 3. choose move only when ownership must transfer. 4. fill a predicate closure. 5. fill a mapping closure. 6. write a closure used with sort_by_key. 7. write a closure that captures a threshold. 8. translate TS arrow functions in array methods. 9. fix a closure capture that conflicts with a later borrow.
Exercises
9 ready
01
pick one
Pick the idiomatic Rust closure that doubles its input.
02
pick one
The closure |x| x * 2 declares no type for x. Pick the
03
pick one
When should you write move || in front of a Rust closure?
04
fill blanks
Fill the closure body. Should evaluate to true for positive
05
fill blanks
Fill the closure body that doubles its input.
06
write a program
Sort vec![3, -1, 4, -5, 2] by ABSOLUTE VALUE ascending using
07
write a program
Declare let threshold = 3;, then a closure
08
write a program
Translate the TypeScript array chain to Rust. Keep only even
09
write a program
This program fails — the closure captures xs by &mut, but