Learning together: Rust – Setting up Rust environment

Originally posted on Medium

Some months ago I challenged myself to learn a language of my interest: Rust. In this and the following articles, I’ll be sharing my experience with the language, its philosophy, its ecosystem, and how to build something useful out of Rust.

Rust

Rust is a modern language born with a focus on performance and safety. Rust is memory-safe, and thread-safe by default, with a rich and powerful type system, generics, metaprogramming, and good functional programming support. It’s also very fast and memory-efficient. Rust is commonly used to build distributed systems, embedded systems, network applications, and WebAssembly applications.

Rust is also being used in big projects like Deno and Tor.

My background

Mostly focused on web applications. I’ve been a backend developer for the last four years working mainly with PHP and, for a brief moment, with Ruby on Rails too.

I’ve also had some academic experience with Python and C/C++, the latter being where I’ve acquired a taste for compiled languages.

First Steps: Meet rustup, rustc, and Cargo

Since programmers will look into official documentation of the language, so did I.

Rust official website

Of course, for most Linux distros versions, you can install Rust directly from official repositories, but if for some reason you need to keep several versions of Rust in your environment or even test the most recent features of the nightly build, you should use rustup.

Rustup

Rustup is a toolchain installer for Rust. With rustup, you can install and manage several versions of Rust. This tool is very easy to install and use.

Bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

If you proceed with the default installation, the rustup will install — in addition to itself — the latest stable version of Rust and Cargo. Once installed, you can see a list of installed toolchains:

rustup toolchain list
Listing installed toolchains

To install a specific version of Rust, you’ll use the install command passing as argument the version that you want:

rustup install 1.43.0  
Installing a specific version of Rust

Cargo

Cargo is a dependency manager for Rust. With Cargo, you can also create packages for the community and make them available on crates.io. Cargo also will be used to start projects and fetch and build our project dependencies.

Cargo commands

Rustc

Rustc is the Rust compiler. In most cases, we’ll build our packages, libraries, or applications by calling Cargo. But we can also call the compiler directly.

For example, you can create a file called main.rs on some directory and compile it with rustic.

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

To compile:

Bash
rustc main.rs

This will create an executable binary called main in the current directory. To run this program, just execute it.

Bash
./main

If everything is ok with your environment, you’ll receive a nice “Hello, world!” from your first program with Rust.

Next steps

We will explore much more of Rust in the following articles. The articles will intend especially to show the main features of Rust and its tools, for those already familiar with programming in general.

For those who want to see more about Rust and the tools presented here, you can always check the book The Rust Programming Language, available on the official website.

Published by


Leave a Reply

Your email address will not be published. Required fields are marked *