Rust programming language is getting ever more popular. It has been voted as the Stackoverflow’s most loved language 4 times in a row. While it takes time for a programming language to create a space, Rust is slowly and steadily getting more adoption across the industry. This is in part due to the great community and detailed documentation available for Rust. In fact, getting started with Rust has never been easier.

But what is so compelling about Rust? Let’s find out in detail.

1 – Intro to Rust Programming Language

Rust is a statically-typed programming language with special emphasis on performance and safety. Rust was originally developed at Mozilla. But now it is open-source and maintained by the Rust Foundation.

Due to its syntactic similarities with C++, Rust is often considered as a systems programming language used for writing systems software such as operating systems, game engines, search engines, industrial automation devices and so on.

However, we can also use Rust for general purpose application programming like building web applications. All in all, Rust is a pretty well-rounded programming language.

2 – What makes Rust different?

There are several angles to look at it.

When compared with dynamically typed languages, Rust provides static type safety. The Rust compiler ensures that you are not mixing data types for variables leading to unpredictable consequences. However, in addition to type safety, Rust also makes sure that you won’t be getting null pointer errors at runtime. This is something that even other type-safe programming languages don’t ensure.

The second angle is around garbage-collection. Many programming languages such as Java have explicit garbage collection mechanisms. However, other systems programming languages such as C++ do not have garbage collection.

In Rust also, there is no garbage collection. Instead, the developers have complete control over low-level details such as storing data on the heap or the stack. This makes Rust suitable for building libraries for other programming languages. You could technically replace performance critical parts of a software with high-performance Rust code.

The third angle applies to other system programming languages. While all such languages ensure efficient memory usage, Rust takes it further using the concept of ownership and borrow checker. These are pretty detailed concepts we will cover in another video. But for the moment, think of them as a middle-ground between garbage-collection and complete manual control of memory.

3 – Where can we use Rust?

  • Since Rust is closer to being a systems-programming language, we can use it to build any low-level application that we can build using C or C++. This includes databases, compilers, and networking code.
  • Another important category of programs you can build using Rust is CLI or Command Line Interfaces. CLIs often need to be efficient and fast because you don’t have a user-interface to keep the user engaged.
  • You can also utilize Rust to write web-assembly code. In case you don’t know about Web-assembly, it is a low-level assembly-like language with a compact binary format that runs with near-native performance. This code can be run by web-browsers. Programs written in languages such as C, C++ and Rust can be compiled to web assembly code and made to run in the browser alongside your Javascript code. More on that in a future post.
  • We can also build performant and robust backend services using Rust. With Rust frameworks such as Actix and Rocket, the task of spinning up a backend service using Rust becomes quite easy.
  • And lastly, we can also use Rust for blockchain development – specifically the Solana blockchain.

4 – Core Ecosystem of Rust

The core of Rust is comprised of a few important components:

First is rustup – a command line tool that can manage Rust versions and associated tools. Rust supports a great number of platforms, so there are many builds of Rust available at any given point of time. The job of rustup is to manage these builds in a consistent way. Basically, we use Rustup to install Rust.

To install Rust, we can simply execute the below command on Mac or Linux machine.

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

This command downloads and installs rustup which in turn installs the latest version of Rust.

To install Rust on a Windows machine, you can follow the link.

The second major component is rustc. This is the Rust compiler. The job of the compiler is to take your source code and produce binary code.

The third important component is Cargo. Cargo is the Rust package manager. Cargo downloads the dependencies of our Rust code, compiles the various packages, generates distributable packages and also uploads them to Crates.

Crates is the Rust community registry. Much like how NPM is for NodeJS. In most applications, we use Cargo to manage our source code and as such we don’t usually use the Rust compiler directly. But Cargo internally uses rustc to perform its job.

5 – Getting Started with Rust Hello World

Let us now write our first Rust program. We will first create a project folder.

$ mkdir rust_demo_project

Now, we can open the project in an IDE. Inside the project folder, we create a file main.rs. Here, rs is the extension of a Rust source file.

Inside the file, we can write our Rust program. See below:

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

The main function is special because this is the first code that runs in every executable Rust program. The main function has no parameters and does not return anything.

Within the function, we will write a simple statement to print a Hello, World message to the console. This is done by using the println and an exclamation mark. The exclamation mark after println signifies that println is a macro.

To run the program, we go to the command prompt and execute the below command.

$ rustc main.rs

This is going to compile our tiny program. If everything went fine, it will create a binary executable file with the name main. We can run the file to get the output message.

$ ./main
Hello, world!

As you can see, running a Rust program is a two-step processcompiling and then running.

Rust is an ahead-of-time compilation language. In other words, we compile a Rust program and hand over the executable to another person to run it on their system. Since it is already an executable, the other person can run it on their system without even installing Rust.

6 – Rust Project using Cargo

While it is pretty straightforward to use the Rust compiler to compile a Rust program, things can get challenging when our programs grow big in terms of functionality and complexity.

We didn’t see it with the Hello, World. But any reasonable program will have some dependencies and managing them can become problematic with using just rustc.

This is where Cargo comes into the picture.

Cargo is the Rust Package Manager. It is also a build system that can help us download dependencies, build our code and build the libraries and so on.

Cargo is part of the Rust installation process using rustup. Hence, we don’t need to install it separately. Either way, we can make sure that Cargo is present by using the below command.

$ cargo --version

If Cargo is present, the above command will output a version number.

Now, we can use Cargo to generate a new project.

$ cargo new rust_cargo_demo

This will create a directory named rust_cargo_demo. If we open it in Visual Studio, we will find two files.

First is the cargo.toml file. TOML stands for Tom’s Obvious, Minimal Language. This is a format similar to something like YAML. See below:

[package]
name = "rust_demo"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

Basically, TOML files describe the Cargo configuration of our project.

The first section of the TOML file contains the package information. Here, we have the name of the package, version and edition. Then, we have the dependencies section. In this case, the section is empty because we don’t have any external dependencies

The second file in our cargo generated project is within the src directory. Cargo creates a separate directory for the source code and within that we have the file main.rs. It contains the same println! macro call to display a Hello, World message on the console.

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

7 – Running the Cargo Rust Project

Our Cargo Rust project is ready. But how do we actually run it?

There are a couple of ways to go about this.

7.1 – The Long Route

The longer route has two steps.

We first execute the cargo build command. This command will basically compile our source code. It creates a folder target/debug containing an executable named rust_cargo_demo. Also, it creates this cargo.lock file. If you open the lock file, there isn’t much right now. But the point of this file is to keep track of the exact dependency versions that we use in the project. This file is managed by cargo and we don’t have to manually do anything here.

Once the executable is ready, we can run the same from the terminal.

./rust_cargo_demo

The program runs and prints the Hello, World message on the console.

7.2 – The Short Route

We can also compress the build and run steps by directly using the cargo run command.

When you execute the command, Cargo compiles our code and also runs the executable that prints the message in the terminal.

There is another helpful command we can use:

$ cargo check

Cargo check basically checks whether our code can be compiled successfully without actually producing an executable. This makes it quite fast to check the status of our program during development. Any compilation errors can be quickly spotted and fixed during development.

8 – Conclusion

Rust will continue to evolve and grow. More and more companies will continue adopting Rust and this will only improve the overall ecosystem. It is definitely worthwhile to spend some time learning the Rust programming language. This guide on getting started with Rust is the first step in this journey.

Want to learn more about Rust? Check out this post on using functions in Rust.

If you have any comments or queries, please feel free to mention them in the comments section below.

Categories: BlogRust

Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

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