A complete guide to get started with Rust - Part 1

If you’re like me and just want to get your hands dirty skipping all the unnecessary stuff, you can jump to Installing Rust

What is Rust?

Rust is a low-level multi-paradigm systems programming language that focuses on performance, type safety and concurrency. It is a compiled language (meaning there’s no runtime like Python) and also very memory safe–ensuring that all references point to valid memory–without using a garbage collector (like Go) or reference counting (like Python). Since it is a compiled language, any errors that may occur are shown during compilation which means that the code is less prone to breaking when in actual use. Rust features static types, immutable variables, zero-cost abstraction, pattern matching, move semantics and many more. Amazon, Discord, Microsoft, Meta and Google have already started using Rust in production. Rust is also used in the Linux kernel!

Why Rust?

After reading the above paragraph, you’d be wondering “yeah, Rust sounds great and all, but why would anyone ever use it? Couldn’t they just use C or C++ or Python?”. Well, you’re not completely wrong but there are many things that make Rust stand out from the crowd.

Rust is blazing fast and memory efficient at the same. Since it is a compiled language, the chance of errors happening after compilation is very low. The Rust compiler checks for type errors, unsafe code and memory leaks. This means that the code will not compile until all of these are fixed. It greatly reduces the possibility of the program crashing when it is deployed. Rust’s rich type system and ownership model guarantee memory-safety and thread-safety.

Rust also has great documentation (and learning tutorials), a friendly yet very strict compiler and top-notch tooling – an integrated package manager and build tool (Cargo), an LSP (Language Server Protocol) called rust-analyzer which provides auto-completion, code formatting and type inspections, and many many more. Another reason is that Rust has a big community of developers who use Rust (called Rustaceans). Then there are crates which are essentially libraries and tools developed by the community.

Installing Rust

Enough reading, lets get our hands dirty! Obviously, to get started with writing Rust code, we need to install the Rust compiler and Cargo. To install these, we’ll use Rustup, which is a tool for managing Rust compilers. Rustup lets us install many different versions of the compiler on a single system and manage it without shooting ourselves in the foot. To install Rustup, you can run the following command on GNU+Linux distros (you’ll need to have curl installed):

curl -sSf https://sh.rustup.rs | sh

or if you’re on Windows, then download the latest binary release here and run the installer.

On Linux, after running the above command you’ll get a screen asking a bunch of questions. In Windows, after the setup finishes running a CMD window will open up. The rest of the steps going forward will be same for both Windows and Linux.

First it’ll show you the default installation options, kinda like this:

Current installation options:

   default host triple: x86_64-unknown-linux-gnu
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

This should be okay for most people but if you want to change any option, hit 2 and then proceed with the rest of the prompts. Those should be self-explanatory so I’m not going to explain that here. One thing you should be careful about is when it asks if you want it to modify the PATH variable. I recommend entering ‘yes’ otherwise you’ll need to add the binary to your path manually, which can be daunting if you’re a beginner.

This should install Rustup, Rust (rustc) and Cargo on your machine provided there are no errors. If there are errors, you can google them or search your issue on StackOverflow or even post on the Rust subreddit.

Hello World!

Now that you have Rust installed (hopefully), we can build our first Rust app. What better to do than print “Hello World”! To get started, we first need to create a project using Cargo. CD into the directory where you want to create your project and then run the following command:

cargo new hello_world

Here hello_world is the name of your project. Rust uses the snake_case for project names. You could theoretically use other cases but Cargo will warn you and its generally a good idea to stick to the standards of the language. Now cd into the newly created directory and open it up in your favourite text editor. I use NeoVim but you could use VS Code. The folder will have this structure:

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

Now you can compile and run your program using:

cargo run

If everything went according to plan, then it should print “Hello World!” in your terminal window. Congratulations🥳, you just made your first program in Rust!

IDE Setup

Now that you’re officially a Rust programmer, you need to setup your IDE to make programmin with Rust easier and also get a lot of useful features like auto-completion, code inspection, debugging, etc… If you’re using Visual Studio Code, you can install the rust-analyzer extension from the official marketplace. If you’re using Neovim, you can use the rust-tools plugin along with the nvim-cmp plugin to get autocompletion. If you want to get an IDE like environment, you can check out my Neovim dotfiles.

If, for some obscure reason, you’re using Intellij or Intellij based IDEs, then there is also a Rust plugin for that.

Welp JetBrains decided to deprecate that plugin and release an IDE specifically for Rust, called RustRover, which you can find here. Its okay for what it does, but it is slow and uses a lot of resources but it does have a lot of cool debugging features. IMHO, I’d recommend using VS Code but if you like JetBrains products, then RustRover might be for you!

That’s the end of part 1 of this guide. In the next part, we’ll see how our Hello World program actually works and also learn a bit about variables. :wq