Improve skills/Functional programming


I recently checked out other rust projects and noticed how well they're made. I'm fairly new to Rust and after seeing how well projects can be structured and ideas implemented in Rust, I'd love to improve myself. I'm coming from the Java world and Rust is functionally oriented so it's a bit of a pain to realize my ideas with a functional-oriented design paradigm. Are there any good resources to improve my Rust skills (I already read the rust book) and maybe some examples on how to go about implementing certain ideas in a functional way?


Is Rust "functionally oriented"?

After a year of using Rust I still use it as if it were C. With an excellent code reviewer, the compiler. With excellent results!

1 Like

Rust can be used in a more functional style to great effect. Some problems lend themselves better to functional idioms. I think the bare-metal type things you do are some of the places where functional idioms aren't as useful. However, to parse code is definitely easier if you write in functional style.


Don't get me wrong. I'm all for picking up as much as I can from the Functional Paradigm.

I'm not sure what you mean by "to parse code". Do you mean for a human to parse and understand the source code?

It is certainly not easier for me to parse a lengthy iter, zip, take, collect, etc with closures thrown in, than good old straight forward procedural if, for, etc.

Hopefully I will get used to it with time. As that seems to be the way the world is headed.


I meant parse as in writing code to parse a source file as part of a compiler frontend.


@Dominlk Rust is a multi-paradigm language. It doesn't force functional programming on you. It is true that it does have support for some functional idioms, but it has just as much support for certain imperative idioms. The one paradigm which Rust doesn't support all that well is Object Oriented programming. However, you can structure your code as imperative loops with mutability everywhere or functional iterator chains with lambdas. Both will work.

If you have code you wrote and you wonder if it is "good" or "idiomatic" Rust, feel free to post in the code review section of the forum. There are many great Rust programmers who are active here and who will give you valuable feedback.

1 Like

I had a vague notion you may have meant that.

I have almost no idea how to make a compiler. It's all magic to me.

Except some years ago I did manage to make a simple compiler for my own simple ALGOL/Pascal/C like language that generated code for x86 and the Propeller MCU from Parallax Inc. Very inefficient, unoptimized code mind you. Based on the article "Let's Build a Compiler" by Jack Crenshaw. Let's Build a Compiler. His exposition uses Pascal, I used C. I was amazed to find that I could get from source text to a runnable binary so easily with so little code.

It's not at all clear to me how using a Functional style in Rust would have made what I wrote to do that any simpler than my old C code.

My take on it was that lexing/parsing the source was the easy part of the job. Turning that into optimized efficient executables being a challenge too far for me.

1 Like

I wouldn't worry too much about functional-oriented, actually. Use it when it's convenient, but one of the great things about Rust is that it makes mutation so much less error prone, and thus procedural code works well too.

It's a good exercise to look at a loop and figure out how to write some for all of it with iterators, though, so I recommend doing that. For bonus points, try all four spots on the spectrum:

  • Entirely imperative
  • A loop that uses an iterator in the body to calculate something
  • Using an iterator to pre-process stuff that's then handled in the body
  • Entirely functional with iterator combinators

The part about Rust I most enjoy could be called a "type-oriented" design paradigm, and I'd suggest focusing on that. I don't know any direct learning resources, but here's a good (albeit really long) article that I thought did a great job explaining the idea:


This is my opinion (obviously) but if you really want to learn functional programming, learn Haskell. This is probably unpopular advice, but it is the best way to get the "full experience" ... because, you are basically forced to do FP. I used Scala for 3-4 years before learning Haskell, and thought I knew some FP ... no, not really. When you do that, you can come appreciate what Rust has to offer in terms of FP, but it won't always be as obvious (but at least you'll know what to look for). Of course, you'll also notice what Rust doesn't have, or at least, isn't as easy to achieve in terms of FP. That said, some things are going to be a lot easier in Rust than in Haskell.

I realize this isn't entirely practical advice. I've been using Haskell for 2 years part-time and feel like I've just gotten to the point where I'm more productive with it than with anything else. I'm still learning Rust so i can't offer much there, but once you've got the basics under your belt (Rust essentials, including ample use of higher-order functions (HOFs) from iterators such as map, foreach, and fold), looks to be a good starting point for the FP side of things.

1 Like

Here's a practical definition of the functional language I discovered. To call something functional language, it should be:

  1. Not a mainstream language I guess.

  2. Has bunch of features those mainstream languages don't have.

  3. Can pass functions like other values.

Back when the python had similar popularities with ruby I can find some people who says the python is a functional language because it has lambda. But now the python inarguably becomes mainstream, and nobody say so again.

I agree that functions-as-values is important in the main functional languages, and that includes "partial application", where the function plus some of its arguments is a function of the rest of its arguments. (If "add(1, 1)" = 2, then "add(1)" = "incr" and "incr(1)" = 2.)

Rust as a functional language seems to me about like Python as a functional language. Both provide some features that help you lean that way - Rust much more than Python - but Rust isn't a functional language any more than it's object oriented. I find it hard to imagine anything working out as a functional language, without some kind of GC. I didn't start with FP but did spend a while at it, and Rust's superficial similarity to OCaml often tricks me into forgetting that I'm working with a C type language.

I'd add implementing a recursive solution to this list, where you're using plain function calls for the repetition. It's not often the best approach, but sometimes invaluable.

1 Like

I think van Rossum himself claimed Python was not inspired by functional languages, but certainly it has more functional facilities than some languages.

There's also distinguishing between functional and pure functional. The former isn't much of a proper category, as you just pointed out - lots of languages have some functional facilities. The latter has ways to enforce referential transparency at the type level, which is why I pointed out Haskell. There are still escape hatches of sorts, but they are onerous enough to use that you'll notice them most of the time, and will typically be strongly encouraged to do things in a functional way because of this purity checking. Rust may have this to some degree with const fn but that's still in development.

Agreed - by the way, with this macro, you can get overhead-free tail-call elimination, which is helpful for doing recursion in practice. Does not currently work for mutual recursion though (only single-function recursion).

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.