DEV Community

Discussion on: Getting Started with Rust

youroff profile image
Ivan Yurov • Edited on

Let's figure out what is functional first. You mentioned Haskell, and even though it's been many years since I played with it, but if I'm not mistaken it doesn't allow mutation of a function arguments. So functions are pure: they take values and return a fresh value. Even though you can formally do the same with Rust, it won't work in practice, here's the example.

Suppose you have a Vector and a function length(list: Vector<T>) -> i32 that returns length. If you call it on a vector, it'll be moved. And none of the subsequent calls on a variable holding that vector will work. Instead Rust encourages using borrowing: length(list: &Vector<T>) -> i32 and passing refs in this case. And in this case it's perfectly fine to call functions (procedures?) with refs multiple times.

Now why I call these procedures: do_something(&mut self) would not return anything, and rather mutate the state of the struct it's attached to. So technically it's not a function, but a procedure that mimicks methods from OOP.

And I guess it's perfectly fine, especially given that it's dictated by the reasons of performance and explicit resources control, but for someone with functional mindset (such as myself) it might be quite painful to adjust.

Thread Thread
leob profile image

I won't argue with you on this particular example, but maybe the result (whether or not you run into these limitations) depends on how you approach the problem - point is I've seen (real world) Rust code on a project which looked and felt way more "FP" than procedural to me. But, it's more like I "dabbled" in Rust than that I could consider myself truly knowledgeable (let alone an expert) in it, I'm definitely not in a position to prove anything.

Some interesting points regarding whether or not Rust can be considered "functional":