Re:How an Engineering Company Chose to Migrate to D
by oconnor663 on Wednesday June 20, 2018 @11:56AM
(#17356539)
I believe the story here is the same in C++. Functions are just a pointer to some compiled code. Closures are an object that represents all the captured variables, in addition to the code. That object has a size, and a destructor, and the variables it's holding might have pointer lifetimes that you need to worry about. In Rust, quite a lot of type system machinery gets invoked when you create a closure, to make sure it doesn't outlive any of the references it's closing over.
Re:How an Engineering Company Chose to Migrate to D
by raphlinus on Wednesday June 20, 2018 @12:02PM
(#17356606)
Yes, it's similar to C++. A Rust `fn()` type is similar to a C++ `(*foo)()` type and at the low level is just a pointer to code. A Rust `Fn` trait object (of which there are several variants) is roughly similar to a C++ std::function and is "fat" in that it can contain data as well as code. Most higher level languages don't make the distinction, buying conceptual simplicity at the cost of potential optimizations when a simple code pointer will do.
[1] https://users.rust-lang.org/t/inner-functions-not-closed-ove...