First of all, I do believe that when learning people should focus on learning about the concepts and then focus on the technology. If you want to learn about programming languages concept I can recommend notes from this course from Stanford (https://cs242.stanford.edu/f19/). However for learning we need some vehicles – languages that show some concepts better than anything else. One of the languages that I’m going to present is used to teach many new concepts that’s why I think it is worth mentioning.
Rust
Being a hackernews favorite language I wanted to know more, so I started learning about Rust and soon I realized why it is such a beloved technlogy (5 years in a row it won the most loved technology among stackoverflow users – https://insights.stackoverflow.com/survey/2020#technology-most-loved-dreaded-and-wanted-languages-loved).
The language
The most important Rust feature is the safety guarantees it gives out of the compilation phase. What it means is that the compiler won’t let you write a program that is memory unsafe. Rust introduced concepts like borrowing (References and borrowing). It also seems that these guarantees don’t bring additional costs (they are offered as “zero-cost” abstraction) and this means that the code will be as fast as C/C++ but much safer.
The safety of the language is cool but I want ergonomics too. The first impression is that Rust is a blend of many different ideas and some of them are very useful:
almost everyhing is an expression
let x = if something { foo() } else { bar() }
pattern matching
let x = 5; match x { 1..=5 => println!("one through five"), _ => println!("something else"), }
mutability by default
let immutable = 1; // cannot do this immutable = 4; // Mutable variable let mut mutable = 1; mutable = 4; mutable += 2;
All of these features make Rust more similar to functional programming language than C/C++. And indeed, Rust roots go to ML family of languages (like Haskell, OCaml). Having programmed in Scala for some time I must say that this functionality feels like home.
Is Rust a functional programming language?
As mentioned in the previous paragraph – Rust borrows a lot from functional programming languages:
- algebraic data types
- default immutability
- expressions
- higher order functions / method chaining
However Rust is not a functional programming language per se. It is worth mentioning that idiomatic functional programming is not always without a cost and for what Rust is supposed to be (a systems programming language) it shouldn’t bring costly abstractions.
Compiler
Rust compiler is amazing! Apart from the obvious design choices that help the user to write safe programs the compiler really tries to help you as best it can. Let’s look at an error that I had today in the software we are building.
error[E0061]: this function takes 1 argument but 0 arguments were supplied --> src/events/parser.rs:32:17 | 32 | None => Err(), | ^^^-- supplied 0 arguments |
Looks normal, but there is more. The compiler suggest a fix! In 90% of the cases this fix works as expected.
help: expected the unit value `()`; create it with empty parentheses | 32 | None => Err(()), | ^^
This was a simple mistake, now something that is not so trivial.
error[E0599]: no method named `user_info` found for reference `&T` in the current scope --> src/accum/accums/item_cooccurence.rs:29:51 | 29 | UserInfoType::VISITOR => Some((*event.user_info().visitor_id).to_string()), | ^^^^^^^^^ method not found in `&T` | = help: items from traits can only be used if the type parameter is bounded by the trait help: the following trait defines an item `user_info`, perhaps you need to restrict type parameter `T` with it: | 27 | fn update<t: events::traits::userinfot="">(&mut self, item_counter: &VisitorItemCounter, event: &T) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ </t:>
What is happening here? This error says that my generic type has no method user_info. Every language can do it. But what comes after the message is a suggestion. Rust found a trait that implements this method and suggests it as an addition. Whoever made such informative errors probably saved thousands of hours of debugging.
Ecosystem
Rust comes with a set of really handy tools for development like:
- cargo and crates (https://crates.io/) – build system and package management
- rustfmt (https://github.com/rust-lang/rustfmt) – seems like a trend for new languages to enforce some styling (which is a good thing in my opinion)
- clippy (https://github.com/rust-lang/rust-clippy) – linter which includes 350 rules that are checked
It feels like people who wrote Rust and all those surrounding tools value quality and ergonomics and it pays off.
Learning
While Rust presents some new concepts and in general it is quite big and advanced language, the learning experience is better than everything I came across.
- Official documentation – The documentation is great (https://doc.rust-lang.org/book/).
- Exercises – Community projects like https://github.com/rust-lang/rustlings make it easy for new programmers to learn the language.
- Community:
- Rust Discord https://discord.gg/ykcZhyq, there is a #beginner channel you can use to ask questions
IDE support
I was quite surprised to have so many good editors available for rust. Visual Studio Code is a prefered option and works great. I also use intellij with Rust plugin and it is also good, but not as nice as vscode. The only thing that is better in intellij is refactoring.
Update: After trying both vscode and intellij with Rust plugin, I like intellij more. Visual Studio Code has many useful extensions but I slightly prefer “batteries included” approach of the latter.
Industry adoption
Created by Mozilla rust has yet to gain wide adoption in the industry. But I think it will happen very soon. The signs are all there. The major players like Microsoft, Facebook, Amazon are betting on this technology (https://www.businessinsider.com/what-is-rust-programming-language-amazon-facebook-discord-love-it-2020-6?IR=T). I believe the main driving force are people who use Rust for hobby – this is where all the love comes from in stackoverflow surveys. But the feelings are not misguided and more and more developers will be happy to work on Rust not only as side projects but also in their professional careers.
Summary
I believe Rust will stay with us for longer. It of course won’t replace Python, but it sure can replace C++ and Java for performance critical pieces of software.