Chrispine Chiedo

Getting started with Rust development

Rust is a systems programming language that guarantees memory and thread safety through a rich type system and a unique ownership model.

Through its ownership model and the borrow-checker, Rust guarantees memory safety without the use of a garbage collector. Rust has consistently been ranked as the most loved programming language on the stackoverflow developer survey since 2016.

Rust is a relatively new systems programming language that came out of Mozilla Research. Since its 1.0 release in 2015, Rust has steadily gained a lot of support and admiration, with some big tech companies investing heavily in the language. The founding members of the Rust Foundation include tech heavyweights like Google, Microsoft, Meta (formerly Facebook), and AWS. And the Linux kernel has finally taken steps to add support for Rust.

Going by the current trend, Rust is definitely poised to become one of the powerhouses in the software engineering space. If you’ve ever wanted to get started with Rust but were wondering how or where to find resources, you’re in the right place.

This is a comprehensive guide on how to get started with the Rust programming language. I hope it sets you on a steady path to mastering the language and serves as a strong starting point for your Rust journey.

Note: Rust has a reputation for having a (relatively) steep learning curve. This guide assumes that you are at least familiar with programming in general (perhaps you’ve worked with other languages like Python or JavaScript before). Since Rust is a systems programming language, having some experience with C/C++ would put you in a better place when starting with Rust (but it’s not a hard requirement). Some familiarity with functional programming would also help (but again, it’s not a hard requirement).

With that out of the way, let’s get started.

Installing Rust and Cargo

The recommended way of installing Rust is via Rustup, which is both an installer and a version management tool. Apart from installing Rust, Rustup also installs Cargo, the Rust build tool and package manager (Cargo is similar to pip or npm, but much more powerful).

You can confirm that your installation is successful by running the following commands in your terminal (or command prompt on Windows):

$ rustc --version

This should return the version of the Rust compiler installed.

$ cargo --version

This should return the version of the Cargo tool installed.

Rust has a 6-week release process (which means there’s a new Rust stable release every 6 weeks). Whenever a new version of Rust is released, you can run the command below to update the Rust version (and the entire Rust toolchain).

$ rustup update

Hello world in Rust

Let’s see how we can create the traditional “Hello world” program in Rust.

In the terminal, run the following Cargo command to create a new binary project (assuming that you’ve navigated to your preferred project directory):

$ cargo new hello-world --bin

This will generate a new directory called hello-world with the following file structure:

$ cd hello-world

$ tree .
├── Cargo.toml
└── src
    └── main.rs

Cargo.toml is the manifest file for a Rust project. It’s where you keep the metadata for your project, as well as your project dependencies.

src/main.rs is where you write your application code. The main.rs file has the following contents:

fn main() {
  println!("Hello, world!");
}         

All rust programs have a main function that acts as the entry point for the program.

To run the program, execute the cargo run command:

$ cargo run

The cargo run command builds and runs the program, printing Hello, world! as shown below:

$ cargo run
   Compiling hello-world v0.1.0
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/hello-world`
Hello, world!

Alternatively, you can build the project first using cargo build and then run it using cargo run.

Other useful Cargo commands are:

For more on the Cargo tool, check out the Cargo book.

Note:

$ cargo new library-project --lib

Rust tooling

The Rust ecosystem provides some great set of tools for working with the language.

Rustfmt

This tool automatically formats Rust code for you (similar to gofmt for Golang). If you install Rust using the rustup tool, you should have rustfmt installed as well. If not, then you can install it as follows:

$ rustup component add rustfmt

You can then run the tool on a cargo project using the following cargo command:

$ cargo fmt

Clippy

This is a linting tool that helps catch common mistakes and improve your Rust code. It ensures that you write idiomatic code that adheres to Rust’s standards.

Similar to rustfmt, if you install Rust using the rustup tool, you should have clippy installed too. If not, then you can install it as follows:

$ rustup component add clippy

You can then run the tool on a cargo project using the following cargo command:

$ cargo clippy

To automatically apply Clippy suggestions, run:

$ cargo clippy --fix

Code Editors

A number of popular code editors/IDEs already offer great Rust support (mainly through extensions/plugins).

VS Code + Rust Analyzer

Through the rust-analyzer extension, VS Code offers great support for Rust development. In my opinion, I think the combination of VS Code and rust-analyzer offers the best Rust development experience at the moment (although things can easily change in the future).

IntelliJ IDEA + Rust plugin

If you are a fan of JetBrains products, there is a Rust plugin for IntelliJ IDEA (the community edition works just fine).

Update: JetBrains now has an official Rust IDE: RustRover.

Neovim

Neovim is a Vim-based text editor that has gained affection from developers in recent years. Check out using Neovim for Rust development to get started.

Resources for Learning Rust

“The Rust Book”

The official Rust Programming Language book is a great resource for learning Rust from the ground up. As you go through the book, you also get to build three projects (of increasing complexity), that really reinforce your knowledge of the language. I’d recommend going through the first nine chapters to get a proper feel of what Rust is all about. I’d also advise that you pay special attention to chapter 4 (Understanding ownership), chapter 6 (Enums and Pattern Matching), and chapter 9 (Error Handling). You may be tempted to ask me why these specific chapters. They cover some of the (most) challenging features of Rust.

Rust by Example

Rust by Example (RBE) is a great resource for folks who like working through examples while learning a new language. RBE offers a collection of runnable examples that illustrate various Rust concepts and standard library features.

Rustlings

Rustlings is a series of small exercises to get you used to reading and writing Rust code. It comes with a rustlings CLI tool that helps in navigating through the exercises from the command line.

If you prefer working through the exercises in an IDE environment, JetBrains offers an adaptation of the Rustlings exercises via the Rustlings plugin. This requires that you install the EduTools plugin as well.

I highly recommend that after going through “the Rust book”, you should consider working through the Rustlings exercises as a next step.

Rust Cookbook

The Rust Cookbook offers a collection of simple Rust code examples that demonstrate good practices to accomplish common programming tasks (for specific use-cases: from cryptography to text processing and network applications).

Rust Cheat Sheet

The Rust Cheat Sheet is a great reference material for some of the important features of the Rust language. It also has a PDF version that you can print and use offline.

Rust track on Exercism

When you start feeling comfortable with Rust, you can try your hand on the Rust track on Exercism. This is a great way of testing your level of understanding of Rust by working through interview-style coding exercises. After submitting your own solutions, you can check out the solutions from other Rustaceans and request for mentorship as well.

Rust Playground (or Rust Explorer)

The Rust Playground gives you a platform to play with Rust without installing any of the necessary tools on your local machine. It’s a great place to build prototypes quickly. It also allows you to easily share code snippets with others.

I recently learnt about a somewhat similar platform: Rust Explorer. I haven’t really played with it much, but it looks like it can be used as an alternative to the Rust Playground.

Rust Books

Apart from the official Rust Book mentioned above, here are other books that I’ve found to be useful when learning Rust:

Rust-focused YouTube Channels

Here are a few YouTube channels that you might find useful (if you are a fan of YouTube videos):

Rust Courses

If you like taking structured courses, then here are some of my favorites:

The Rust Community

If you’d like to interact with fellow Rustaceans within the wider Rust ecosystem, here are a few places to look at. Rust is known to have a very welcoming (and inclusive) community.

Conclusion

Rust is rapidly gaining popularity. Even the folks at WIRED think that Rust is taking over tech. I hope that this guide helps you in your journey to becoming a productive Rust developer.

May the (Ferris) force be with you!

Share on: