I think two of the biggest differences are:
- Because the type and memory systems make it possible to skip heap allocation if you’re careful, it’s “idiomatic” to prefer avoiding techniques that still require it. This means avoiding long-lived closures. Note that closures still are used very heavily, but typically aren’t passed around between many owners like in Lisp or JavaScript.
- Similarly, because Rust can give you incredible safety and speed if you use it a certain way, people tend to write with interior mutability very often. However, unlike in most languages, Rust’s type systems is a tremendous help to the programmer with mutability, and it’s much, much more likely that you’ll see interior mutability done right. It allows you to program in a style that feels very functional even while using many imperative components. A lot of your code still ends up as basic pattern matching and simple closures.
No, never, because it makes the state machine incredibly hard to follow and an absolute nightmare to debug. From experience.
That's really not the case. In fact I'd say the opposite, the focus on ownership, limitations of the borrow checker (partial borrows through function calls) and lack of inheritance makes "object oriented programming" rather difficult. That functions are commonly namespaced through traits or structs does not intrinsically make it "object oriented".
It has a number of functional APIs (many standard structures have various HoFs for manipulating them and combining them), although the ownership and borrow checking bits can also make them somewhat less convenient.
(Before Scala, research was mostly trying to show the opposite: how OO is an edge case of FP, but that didn't work quite so well.)
I mean, Common Lisp has had CLOS since at least 1984.
There was a recent post comparing rust to haskell https://www.fpcomplete.com/blog/2018/10/is-rust-functional. And tldr is yes it lends itself to a functional style...
Rust does a good job of supporting a functional style efficiently. It's also considered idiomatic to code functionally. Probably the biggest, obvious exception is that Rust embraces mutability because it's statically safe to do so i.e. you aren't as susceptible to the traditional disadvantages of mutability.
> You do not use inheritance nor do you have objects
That's only partially true though. Rust does support interface inheritance and polymorphism in its trait system. And enums are a closed form of sub-typing. RAII is also idiomatic and I'd argue that many would class that as object-oriented.
The way I'd characterise the OO approach in Rust is that the team looked at the experiences of using OO in other languages and picked out what they considered the valuable pieces and left the dangerous ones (like implementation inheritance).
It supports no more than Haskell's typeclasses (rather less in fact), are typeclasses an object system?
> And enums are a closed form of sub-typing.
Rust enums are pretty standard sum types, a staple of statically typed functional languages.
> RAII is also idiomatic and I'd argue that many would class that as object-oriented.
That's defensible but setting one hell of a low bar on the concept of object orientation.
No, did someone say they were?
> Rust enums are pretty standard sum types, a staple of statically typed functional languages
Indeed
I'm not entirely sure what your point is. Functional and OO aren't mutually exclusive.
They must by definition be. I disagree vehemently with your statement. If you were correct, then there would be absolutely no point to programming "functionally", it would be absurd. Might as well go back to programming in Java or C++, which is utterly counterproductive.
Java and C++ are really imperative + OOP, not pure OO. The closest to pure OO are going to be languages like Smalltalk which still has some imperative concepts and some concepts from functional programming (closures and higher order functions via blocks, in particular).
Scheme allows for object oriented programming, though not directly in the base language spec. You have to build out some infrastructure for it, but then it's quite effective for it.
You're implying that Rust's traits "partially" make for an object system. Given Rust's trait are essentially a restricted form of Haskell's typeclass, these would by your assertion "partially"+ make for an object system as well.
That doesn't really have anything to do with whether one can or cannot call Haskell's typeclasses an object system.
lorry.pickup(&rustfest_envelope);
lorry.done();
looks like methods on an object ("lorry") to me. Is it something else?Thank you for the insight.
Syntax is just syntax. Imagine a function that takes two 32-bit numbers, and adds them together:
fn foo(x: i32, y: i32) -> i32 {
x + y
}
You'd call it like this: foo(5, 6) // would give back 11
However, this is just syntax. There's no reason this can't be 5.foo(6)
It's purely a different way of calling the same thing. The first argument goes before the dot.In some languages, this is 100% interchangable. In Rust, it's not 100%, but it's pretty true. For example, the wrapping_add method adds two numbers together, wrapping around if the number is too large. These are equivalent:
let x: i32 = 5;
assert_eq!(x.wrapping_add(5), 10);
assert_eq!(i32::wrapping_add(x, 5), 10);
You can use either method or function call style.These aren't objects, they're just primitive 32 bit numbers. There's no heap allocation, there's no extra bookeeping info.
There are some more details, but that's the basic idea. Does that make sense?
It's a travesty this syntax or anything resembling object-oriented programming is allowed.
By the by, the best programming languages aren't ones where there is more than one way to do it, but ones where there is one and exactly one way to do any given thing and the reason is simple: the code is easier to understand and therefore enhance or debug for anyone proficient in that language; in addition, it reduces the total number of mistakes in software written in such a language. AWK is one example of such a language.
So it's a multi-paradigm language where any manner of coding is possible.