Closure with Box<T> arguments in Rust

Solution 1:

Here is an example how you can do that:

fn main() {
    let a = Box::new(5);
    let double = |x: Box<i32>| 2 * *x;
    let b = double(a);
    print!("{b}")
}

First, you need to specify the closure parameter type in this case. Instead of Box<i32>, you can also write Box<_>.

Next, you need to get the value owned by the Box via *x.