Fix make_adder and make_op, which return a bare dyn Fn(i32) -> i32 and do not compile
make_adder(k) should return a function that adds k to its argument, and make_op(add, k) one that adds k when add is true and subtracts it otherwise. Neither compiles: rustc reports error[E0746]: return type cannot be a trait object without pointer indirection. Fix both functions and say when each return type is the right choice.
Requirements:
make_adder(3)(4)is7,make_op(true, 10)(15)is25,make_op(false, 10)(15)is5.make_adderallocates nothing on the heap.make_opkeeps returning one of two separate closures, chosen byadd.
fn make_adder(k: i32) -> dyn Fn(i32) -> i32 {
|x| x + k
}
fn make_op(add: bool, k: i32) -> dyn Fn(i32) -> i32 {
if add { |x| x + k } else { |x| x - k }
}Find and fix the bug.
A bare dyn Fn has no size known at compile time (E0746). Return impl Fn(i32) -> i32 with move |x| x + k: one hidden type, no allocation. Branches returning different closures need Box<dyn Fn(i32) -> i32>: allocation plus a dynamic call.
- Thinking
impl Fnlets two branches return different closures - Forgetting
move, so the returned closure borrows the dead parameterk(E0373) - Boxing every returned closure, paying an allocation where
impl Fnwould do
- Why do two closures with identical text in the branches of an
ifstill have different types? - When would a function pointer type
fn(i32) -> i32work as the return type instead?
There are three errors here, and rustc reports them one after another.
dyn Fn(i32) -> i32is a type with no size known at compile time: different closures capture different things and take different space. A value of such a type cannot be returned by value — hence E0746.- After switching to
impl Fn(i32) -> i32, the closure|x| x + kstill borrows the parameterk, which dies when the function returns:error[E0373]: closure may outlive the current function, but it borrows `k`. It needsmove. - The two closures in the branches of
make_ophave different unnamed types, even with identical text, whileimpl Fnstands for exactly one concrete type:error[E0308]: `if` and `else` have incompatible typeswith the noteno two closures, even if identical, have the same type. This one needsBox<dyn Fn>.
fn make_adder(k: i32) -> impl Fn(i32) -> i32 {
move |x| x + k // move: the closure gets its own copy of k
}
fn make_op(add: bool, k: i32) -> Box<dyn Fn(i32) -> i32> {
if add {
Box::new(move |x| x + k)
} else {
Box::new(move |x| x - k)
}
}
fn main() {
let add3 = make_adder(3);
let ops = [make_op(true, 10), make_op(false, 10)];
println!("{} {} {}", add3(4), ops[0](15), ops[1](15)); // 7 25 5
}impl Fn is the default choice: behind it stands one concrete closure type, stored right in the returned value, and the call is static and usually inlined. The caller cannot name that type, and does not need to. Box<dyn Fn> is needed when the type is chosen at run time: different closures in the branches, or closures kept together, for example in a Vec<Box<dyn Fn(i32) -> i32>>. The price is one heap allocation per closure and an indirect call through the method table.
The task asks for two separate closures, but without that condition Box can be avoided: a single closure move |x| if add { x + k } else { x - k } is one type, and impl Fn fits. A function pointer fn(i32) -> i32 works only for closures that capture nothing, and both of these capture k.