DEV Community

Discussion on: Functional Programming in Rust

dystroy profile image
Denys Séguret

An immediate question is "Why Functional programming in Rust?".

Rust's model is a different approach which solves many problems that FP solves. Accidental side effects don't normally happen in Rust and data are immutable by default. You're kind of forced to a modular and maintainable design too and risk-free concurrent execution comes easily.

So, why would you need or want FP in Rust ? Just as a kind of more elegant style ?

citizen428 profile image
Michael Kohl

An immediate question is "Why Functional programming in Rust?".

It's a good question too. While bringing functional programming paradigms into other languages has been quite popular in the past few years, it can have its drawbacks. For example, if data structures were not designed with immutability in mind, you may end up with many copy operations because the compiler/interpreter can't make the guarantees necessary for structural sharing.

Two or so years ago I did a bit more Rust and when I benchmarked functional vs more imperative solutions the latter always came out faster, sometimes quite significantly. Granted, that may not matter for a lot of use cases, and the combination of raw performance while still offering high-level APIs is one of Rust's main selling points for me, but it's a tradeoff that needs to be considered.

While it's true that many languages nowadays can be written in many different styles, it's worth looking into how well supported different paradigms are in the language's implementation.