DEV Community

Discussion on: Why Learn Rust?

Collapse
 
dmbaturin profile image
Daniil Baturin

For many people, Rust is their first language with an expressive type system, mostly because it has a big name behind it, and it has a unique gimmick among languages that made it to production use (linear types).

However, it's not the first one. Do you know what Rust was written in before it became self-hosted? It was in OCaml, and its type system is seriously influenced by it. OCaml also has its own unique advantages that Rust doesn't have, like an extensible REPL (while it can also compile to native code).
It's also statically typed without having to write any types by hand because its type inference is decidable.

Rust's traits is a clone of Haskell type classes. People who knew a bit of Haskell never had a problem picking that up.

There are also type systems more expressive than that of Rust. ATS has a borrow checker and a system of dependent types for example.

Rust is good for what it is, but it's neither the first, nor the only, and definitely not the last statically typed functional language. Not even the best for every use case.

Collapse
 
stevepryde profile image
Steve Pryde

This is true. Rust intentionally borrows features from many other languages.

I don't think any of these other languages have anything like the borrow checker though. It is specifically the way Rust allows mutation or aliasing but never both at once that is key to it avoiding many common programming mistakes.

I also don't consider OCaml or Haskell mainstream, but perhaps that is subjective.

Collapse
 
dmbaturin profile image
Daniil Baturin

They don't have a borrow checker, but neither a borrow checker is required for memory safety or benefits of structural sharing. ML and Haskell make everything immutable by default and offer mutable cells as a special case.

The only thing you absolutely need some form of linear types (borrow checker) for is memory safety without a GC. But many applications can live just fine with a GC. Even in Rust, a fallback to runtime-checked ref cells is required in at least some cases.

My point is that what we really should be saying is "learn about typed functional languages". Some people will not like OCaml or Haskell or find it suitable for their use case, but that's equally true for Rust.

Thread Thread
 
stevepryde profile image
Steve Pryde

I tend to take the opposite approach and say that rust provides safety without needing to go for a full functional language. I think both functional languages and rust have similar concerns. I just much prefer the way Rust approaches it because I'm not forced into one paradigm (functional vs imperative).

I use functional programming techniques a lot but I never want to be limited to them. It's one paradigm. It's not the only one.

Memory safety is only a tiny part of why I like Rust. Mostly I like the correctness and safety aspects. I think it has a good balance of features from many mainstream sources.

Thread Thread
 
dmbaturin profile image
Daniil Baturin

If Rust works for you, then it works for you. The problem is, you don't know what else is in the store. And you wouldn't be worse off if you knew. ;)

OCaml definitely doesn't force you into the functional paradigm. It even has an OO system with type inference for objects.
Among ML descendants, there's also Swift that tries it best to pretend it's not a functional language to avoid scaring Objective-C users off.

I never regret learning Rust. I also don't use it in my current projects, though I may use if it in the future.