3 ms·
what about `fn(args) => exp`? Can be simplified as `fn arg => exp(arg)` for simple ones..
by entha_saava 6y ago
what about `fn(args) => exp`? Can be simplified as `fn arg => exp(arg)` for simple ones..
- chrismorgan 6y agoThat works for JavaScript, because functions are just functions. But in Rust, closures are different from functions, though they all implement the Fn, FnMut or FnOnce traits. Functions don’t close over any state (so you can cast them to a 'static function pointer), while closures can. Using the fn keyword for closures would thus muddy the conceptual waters—though you could argue that the names of the Fn* traits has already done that—and potentially hinder future language design. I feel a stronger argument is that it’s also markedly longer and more visually noisy; `x.map(|x| …)` is seven characters shorter than `x.map(fn x => …)` and eight than `x.map(fn(x) => …)`, and to some extent words are more distracting than symbols. Pipes isn’t perfect, but given the consistent philosophies of the Rust language, I am not aware of any better option. But it’s definitely a fairly subjective matter.