Rust Closures Quiz
Master Rust's closure system for capturing environment variables and creating flexible function-like constructs
Question 1
What is a closure in Rust?
Question 2
What does this basic closure example do?
let add = |a, b| a + b;
let result = add(2, 3);Question 3
What are closure capture modes?
Question 4
What does this closure capture by reference example do?
let x = 5;
let closure = || println!("{}", x);
closure();Question 5
What does this closure capture by mutable reference example do?
let mut x = 5;
let mut closure = || {
x += 1;
println!("{}", x);
};
closure();Question 6
What does this closure capture by value example do?
let x = vec![1, 2, 3];
let closure = move || println!("{:?}", x);
closure();Question 7
What is the Fn trait?
Question 8
What is the FnMut trait?
Question 9
What is the FnOnce trait?
Question 10
What determines which Fn trait a closure implements?
Question 11
Which Fn traits does this closure implement?
let x = 5;
let closure = || println!("{}", x);Question 12
Which Fn traits does this closure implement?
let mut x = 5;
let closure = || {
x += 1;
println!("{}", x);
};Question 13
Which Fn traits does this closure implement?
let x = vec![1, 2, 3];
let closure = move || println!("{:?}", x);Question 14
What is closure type inference?
Question 15
What does this closure type annotation example show?
let add: fn(i32, i32) -> i32 = |a, b| a + b;Question 16
How do you store a closure in a struct?
Question 17
What does this closure storage example do?
struct Processor<T> {
func: T,
}
let p = Processor {
func: |x| x * 2,
};Question 18
What does this trait object closure storage example do?
struct Processor {
func: Box<dyn Fn(i32) -> i32>,
}
let p = Processor {
func: Box::new(|x| x * 2),
};Question 19
What is a move closure?
Question 20
When would you use a move closure?
Question 21
What does this move closure example do?
let x = vec![1, 2, 3];
let closure = move || println!("{:?}", x);
closure();
// println!("{:?}", x); // This would errorQuestion 22
What are closure return types?
Question 23
What does this closure return type example show?
let add = |a: i32, b: i32| -> i32 { a + b };Question 24
What is the difference between closures and functions?
Question 25
How do you pass a closure as a function parameter?
Question 26
What does this closure parameter example do?
fn apply<F>(f: F, x: i32) -> i32
where F: Fn(i32) -> i32 {
f(x)
}
let result = apply(|n| n * 2, 5);Question 27
What is closure lifetime?
Question 28
What does this closure lifetime example demonstrate?
fn create_closure() -> impl Fn() -> i32 {
let x = 5;
move || x
}Question 29
What are higher-order functions?
Question 30
What does this higher-order function example do?
fn create_multiplier(factor: i32) -> impl Fn(i32) -> i32 {
move |x| x * factor
}
let double = create_multiplier(2);
let result = double(5);Question 31
What is closure sugar?
Question 32
What is the performance impact of closures?
Question 33
What is a closure environment?
Question 34
What does this environment capture example show?
let a = 1;
let b = 2;
let closure = || {
let c = 3;
a + b + c
};Question 35
What are diverging closures?
Question 36
What does this diverging closure example do?
let closure: Box<dyn Fn() -> !> = Box::new(|| panic!("Error!"));Question 37
What is closure coercion?
Question 38
What does this closure coercion example show?
fn take_fnmut<F: FnMut()>(mut f: F) { f(); }
let mut x = 5;
let closure = || x += 1;
take_fnmut(closure); // FnMut coerced from FnOnceQuestion 39
What are closure captures in patterns?
Question 40
In a scenario where you're designing a caching system that needs to store computation functions, how would you handle storing closures that capture different amounts of state, and what are the trade-offs of each approach?
