What is the difference between Closure::new and Closure::wrap?
Both are used to create closure but I wanna know when to use these functions.
What is the difference between Closure::new and Closure::wrap?
Both are used to create closure but I wanna know when to use these functions.
It's quite simple, all that
Closure::newdoes is put the closure in aBox, unsize it and delegate towrapas you can see by looking at the code:So use
Closure::newwhen you have a bare Rust closure or function and usewrapwhen your closure is already a trait object contained in aBox(Box<dyn Fn>orBox<dyn FnMut>) asnewwould wrap it in an additionalBoxand thus create unnecessary indirection.