Rust Closures Quiz

Rust
0 Passed
0% acceptance

Master Rust's closure system for capturing environment variables and creating flexible function-like constructs

40 Questions
~80 minutes
1

Question 1

What is a closure in Rust?

A
An anonymous function that can capture variables from its environment
B
A named function
C
A macro
D
A trait
2

Question 2

What does this basic closure example do?

rust
let add = |a, b| a + b;
let result = add(2, 3);
A
Creates a closure that adds two numbers, result is 5
B
Compilation error
C
Creates a function named add
D
Runtime error
3

Question 3

What are closure capture modes?

A
Ways closures capture variables: by immutable reference, mutable reference, or value
B
Ways to define closure parameters
C
Ways to return values from closures
D
Capture modes don't exist
4

Question 4

What does this closure capture by reference example do?

rust
let x = 5;
let closure = || println!("{}", x);
closure();
A
Captures x by immutable reference and prints 5
B
Takes ownership of x
C
Compilation error
D
Runtime error
5

Question 5

What does this closure capture by mutable reference example do?

rust
let mut x = 5;
let mut closure = || {
    x += 1;
    println!("{}", x);
};
closure();
A
Captures x by mutable reference and prints 6
B
Captures x by value
C
Compilation error
D
Runtime error
6

Question 6

What does this closure capture by value example do?

rust
let x = vec![1, 2, 3];
let closure = move || println!("{:?}", x);
closure();
A
Moves x into closure and prints [1, 2, 3]
B
Borrows x immutably
C
Compilation error
D
Runtime error
7

Question 7

What is the Fn trait?

A
Trait for closures that capture by immutable reference only
B
Trait for closures that can mutate captured variables
C
Trait for closures that take ownership of captured variables
D
Fn trait doesn't exist
8

Question 8

What is the FnMut trait?

A
Trait for closures that can mutate captured variables
B
Trait for closures that capture immutably
C
Trait for closures that take ownership
D
FnMut trait doesn't exist
9

Question 9

What is the FnOnce trait?

A
Trait for closures that can only be called once and take ownership of captured variables
B
Trait for closures that can be called multiple times
C
Trait for closures that borrow immutably
D
FnOnce trait doesn't exist
10

Question 10

What determines which Fn trait a closure implements?

A
How the closure captures variables from its environment
B
The closure's parameter types
C
The closure's return type
D
The compiler randomly chooses
11

Question 11

Which Fn traits does this closure implement?

rust
let x = 5;
let closure = || println!("{}", x);
A
Fn and FnMut and FnOnce
B
FnMut and FnOnce
C
FnOnce only
D
None
12

Question 12

Which Fn traits does this closure implement?

rust
let mut x = 5;
let closure = || {
    x += 1;
    println!("{}", x);
};
A
FnMut and FnOnce
B
Fn and FnMut and FnOnce
C
FnOnce only
D
None
13

Question 13

Which Fn traits does this closure implement?

rust
let x = vec![1, 2, 3];
let closure = move || println!("{:?}", x);
A
FnOnce only
B
Fn and FnOnce
C
FnMut and FnOnce
D
All three
14

Question 14

What is closure type inference?

A
Rust infers closure parameter and return types from usage
B
Closures don't have types
C
Types must always be explicitly specified
D
Type inference doesn't apply to closures
15

Question 15

What does this closure type annotation example show?

rust
let add: fn(i32, i32) -> i32 = |a, b| a + b;
A
Explicit function pointer type annotation
B
Closure type annotation
C
Compilation error
D
Runtime error
16

Question 16

How do you store a closure in a struct?

A
Use generic types or trait objects (Box<dyn Fn>)
B
Closures cannot be stored in structs
C
Use fn pointers
D
Use macros
17

Question 17

What does this closure storage example do?

rust
struct Processor<T> {
    func: T,
}

let p = Processor {
    func: |x| x * 2,
};
A
Stores closure in generic struct using type inference
B
Compilation error due to unknown closure type
C
Stores function pointer
D
Runtime error
18

Question 18

What does this trait object closure storage example do?

rust
struct Processor {
    func: Box<dyn Fn(i32) -> i32>,
}

let p = Processor {
    func: Box::new(|x| x * 2),
};
A
Stores any Fn closure that takes i32 and returns i32
B
Compilation error
C
Stores function pointer
D
Runtime error
19

Question 19

What is a move closure?

A
A closure that takes ownership of captured variables
B
A closure that moves data
C
A closure that cannot capture variables
D
Move closures don't exist
20

Question 20

When would you use a move closure?

A
When closure needs to outlive the current scope or be sent to another thread
B
When you want to borrow variables
C
When closure doesn't capture anything
D
Move closures are never needed
21

Question 21

What does this move closure example do?

rust
let x = vec![1, 2, 3];
let closure = move || println!("{:?}", x);
closure();
// println!("{:?}", x); // This would error
A
Moves x into closure, making x inaccessible after move
B
Borrows x immutably
C
Compilation error
D
Runtime error
22

Question 22

What are closure return types?

A
Closures can return any type, inferred or explicitly specified
B
Closures cannot return values
C
Closures must return ()
D
Return types don't apply to closures
23

Question 23

What does this closure return type example show?

rust
let add = |a: i32, b: i32| -> i32 { a + b };
A
Explicit return type annotation for clarity
B
Required syntax
C
Compilation error
D
Runtime error
24

Question 24

What is the difference between closures and functions?

A
Closures can capture environment, functions cannot
B
Functions can capture environment, closures cannot
C
They are identical
D
Functions don't exist
25

Question 25

How do you pass a closure as a function parameter?

A
Use generic types or trait bounds like F: Fn(i32) -> i32
B
Closures cannot be function parameters
C
Use fn pointers only
D
Use macros
26

Question 26

What does this closure parameter example do?

rust
fn apply<F>(f: F, x: i32) -> i32
where F: Fn(i32) -> i32 {
    f(x)
}

let result = apply(|n| n * 2, 5);
A
Accepts any Fn closure and applies it to 5, result is 10
B
Compilation error
C
Accepts function pointers only
D
Runtime error
27

Question 27

What is closure lifetime?

A
Closures cannot outlive the variables they capture
B
Closures live forever
C
Closures have no lifetime restrictions
D
Lifetime doesn't apply to closures
28

Question 28

What does this closure lifetime example demonstrate?

rust
fn create_closure() -> impl Fn() -> i32 {
    let x = 5;
    move || x
}
A
move closure takes ownership, allowing return from function
B
Compilation error due to lifetime issue
C
Closure borrows x immutably
D
Runtime error
29

Question 29

What are higher-order functions?

A
Functions that take other functions as parameters or return functions
B
Functions that are higher level
C
Functions that cannot be called
D
Higher-order functions don't exist
30

Question 30

What does this higher-order function example do?

rust
fn create_multiplier(factor: i32) -> impl Fn(i32) -> i32 {
    move |x| x * factor
}

let double = create_multiplier(2);
let result = double(5);
A
Returns a closure that multiplies by factor, result is 10
B
Compilation error
C
Returns a function pointer
D
Runtime error
31

Question 31

What is closure sugar?

A
Syntactic sugar that makes closures more ergonomic
B
Sugar added to closures
C
Closures that are sweet
D
Closure sugar doesn't exist
32

Question 32

What is the performance impact of closures?

A
Zero cost when inlined, minimal overhead when trait objects are used
B
Closures are always slow
C
Closures have no performance impact
D
Performance depends on capture mode
33

Question 33

What is a closure environment?

A
The variables captured by the closure
B
The scope where closure is defined
C
The closure's parameters
D
Closure environment doesn't exist
34

Question 34

What does this environment capture example show?

rust
let a = 1;
let b = 2;
let closure = || {
    let c = 3;
    a + b + c
};
A
Closure captures a and b by reference, c is local
B
Closure captures all variables
C
Compilation error
D
Runtime error
35

Question 35

What are diverging closures?

A
Closures that never return (diverge)
B
Closures that return multiple values
C
Closures that panic
D
Diverging closures don't exist
36

Question 36

What does this diverging closure example do?

rust
let closure: Box<dyn Fn() -> !> = Box::new(|| panic!("Error!"));
A
Creates a closure that never returns due to panic
B
Compilation error
C
Creates a normal closure
D
Runtime error
37

Question 37

What is closure coercion?

A
Automatic conversion between closure traits when possible
B
Forcing closures to change types
C
Coercion doesn't apply to closures
D
Closure coercion doesn't exist
38

Question 38

What does this closure coercion example show?

rust
fn take_fnmut<F: FnMut()>(mut f: F) { f(); }

let mut x = 5;
let closure = || x += 1;
take_fnmut(closure); // FnMut coerced from FnOnce
A
FnMut closure can be passed where FnOnce is expected
B
Compilation error
C
FnOnce closure cannot be coerced
D
Runtime error
39

Question 39

What are closure captures in patterns?

A
How closures capture variables in match expressions
B
Capturing patterns in closures
C
Pattern matching in closures
D
Closure captures in patterns don't exist
40

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?

A
Use Box<dyn Fn> for maximum flexibility but with vtable overhead, or generics for zero-cost but monomorphization bloat
B
Always use function pointers for consistency
C
Avoid closures and use global functions
D
Store closures as strings and eval them

QUIZZES IN Rust