4 ms·
Close. :-) Closures are anonymous types which implement one of the Fn traits (which are not anonymous). Traits are usually not implemented with vtables; they ar
by tatterdemalion 10y ago
Close. :-) Closures are anonymous types which implement one of the Fn traits (which are not anonymous). Traits are usually not implemented with vtables; they are usually monomorphized and statically dispatched (this includes most higher order functions). If you need dynamic dispatch, they can be cast into "trait objects" with vtables. This is a pretty big deal - static dispatch is why functionalish abstractions like map and filter can compile to a very tight loop.
- Animats 10y agoAnd you wonder why C programmers have trouble with Rust.
- akiselev 10y agoNo one in their right mind is wondering that. Going from "go ahead, do whatever you want" memory management to Rust's ownerhsip system or object oriented programming to trait based programming isn't something anyone excepts to happen without some perseverance.
- Animats 10y ago"Expects", I think. Ask who really needs Rust. People who write code that has to work and causes real trouble when it doesn't. That includes embedded system programmers. They're typically into C and can use an oscilloscope. They seldom know C++ and have never heard of Haskell. But they can lay out a circuit board and get it to work. Rust needs to be accessible to those people.
- deleted 10y ago[deleted]
- kibwen 10y agoRust's closure model is literally just C++'s with an extra dose of memory safety on top.
- Manishearth 10y agoAlmost none of the above is something you actually need to know when programming in Rust. You just need to know how closures work at a high level, which is pretty straightforward. Rust closures are basically the same as C++ closures. If you're really worried about the generated code generally iterator code with closures compiles down to the same thing as the imperative code. This isn't a hidden complexity unique to Rust; C code these days is pretty far removed from the generated asm too.
- Veedrac 10y agoI strongly disagree. Though it is possible to fudge through working Rust code without understanding the principles that govern Rust, which is probably more than you can say for C++, not understanding these things is going to make life a lot harder when you do run up against them. An example: https://www.reddit.com/r/rust/comments/5iwyt9/how_to_accomplish_this_without_the_compiler/ https://www.reddit.com/r/rust/comments/5iwyt9/how_to_accompl... You might not need to know what "vtable" means, but the differences between, for example, `T: Fn()` and `T = Fn()` are pretty important.
- ucarion 10y agoAh, thanks for catching that subtle-but-important mistake. :)