DEV Community

loading...
Cover image for Why Learn Rust?

Why Learn Rust?

stevepryde profile image Steve Pryde ・3 min read

In this article I want to share my thoughts on what Rust is really good for, why it exists, and what it can offer you that other languages might not.

I find that when I learn anything new, I first need to have a reason for learning it. There needs to be a purpose. What can it offer me? The same is true for programming languages. Learning anything can bring challenges and some setbacks, and overcoming those is going to be a lot easier if we can answer that simple question... "Why?"

Bugs

I hate bugs. I hate using software that has bugs. I hate it when programs crash, or go slow, or use all of my computer's memory, or leave my data vulnerable to security flaws.

In 2020 we are still using and writing software that has the same bugs, the same mistakes, the same security issues, that were present 20 or even 30 years ago.

The human factor

Most modern programming languages focus on making coding easier, and many have succeeded. But unfortunately they often make coding bugs easier too. We need a change.

Most programming languages still leave it up to the programmer to "get everything right" and it is up to us to remember to put in all of the right safety checks. But programmers are lazy (myself included). We take shortcuts and tell ourselves we'll fix it later. We never fix it later. And so we still have software that crashes.

We tell ourselves that bugs are the programmer's fault. We just need to write better code. Follow better practices. Use better tooling. Become better programmers. Become more experienced.

But is that enough? Even experienced programmers can (and often do) make the same mistakes. We're all human.

Today we have coding guidelines, formatters, linters, static analysis, profilers, benchmarks, unit tests, entire test frameworks, CI/CD, best practices, architects, analysts, experienced software engineers, and open source. And these are all fantastic things that really do help. But they're still not enough.

We still make the same mistakes. Null pointer access, double-free, use-after-free, data races, undefined variables, type mismatch, conversion errors, unhandled exceptions, uncaught errors, and a whole lot more!

Enter Rust

This is why Rust exists. It won't prevent all bugs. No language will do that. But it can prevent entire classes of bugs and that is something we haven't really seen in this industry before in a relatively mainstream language. No null pointers. No data races. Sum types (for more accurate modeling). Exhaustive pattern matching. And a strong focus on correctness and safety by default.

I believe the whole point of Rust, its reason for being, is to allow programmers like you and me to create software that does exactly what we want, with less bugs, and runs faster. By making many types of bugs impossible, I think Rust really does deliver on that goal. Yes it has some new concepts to learn that may be different from other languages, but in return it enables you to create software that is more reliable, with better error handling, and awesome performance. It is not uncommon to spend a little more time fixing errors reported by the compiler, and then much less time debugging.

Last but not least, learning Rust will teach you a lot about common programming mistakes and how to avoid them, even when you're programming in other languages. It will change the way you code everywhere, for the better.

Discussion (7)

pic
Editor guide
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 Author

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 Author

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.

Collapse
mtnrbq profile image
mtnrbq

Until I read your article I'd not heard or noticed Rust was an FP language, possibly due to being in a bit of a .net bubble ;)
I currently use F# for FP, and multi-paradigm programming in .net, but will definitely check out Rust - Thanks!

Collapse
stevepryde profile image
Steve Pryde Author

Rust is multi-paradigm. It allows you and often rewards you for using a more FP style but I'm not sure I would call it an "FP language". You can use an imperative style as well or mix and match as you choose