Introduction to Rust

What is Rust?

What is Rust?

Rust is a modern systems programming language created in 2006 and published in 2015 by Graydon Hoare, a software developer at Mozilla Research, as a personal project. It is a systems programming language focusing on performance, memory safety, and safe concurrency. Developers largely see it as a replacement for the mature C and C++, languages as old as they are powerful.

Key features of Rust

Rust is a modern systems programming language focusing on safety, performance, and concurrency. It was designed to address common programming pitfalls and challenges, particularly in systems-level programming. Here are some key features that make Rust stand out:

  1. Memory Safety without Garbage Collection: Rust introduces a unique ownership system that enforces strict rules about accessing and managing memory. This prevents common bugs like null pointer dereferences, dangling pointers, and data races without needing a garbage collector.

  2. Ownership, Borrowing, and Lifetimes: Rust's ownership system ensures that memory is managed efficiently and safely. Variables have ownership of their data, and the concept of borrowing allows functions to temporarily use data without taking ownership. Lifetimes ensure that references remain valid and prevent dangling references.

  3. Zero-Cost Abstractions: Rust allows high-level programming constructs and abstractions without sacrificing performance. Its "zero-cost abstractions" philosophy means that the abstractions used at the high level compile into efficient machine code with minimal overhead.

  4. Concurrency and Parallelism: Rust's ownership and borrowing model also enables safe concurrency and parallelism. The Send and Sync traits ensure that data can be safely transferred between threads and shared between threads. The async/await syntax simplifies asynchronous programming.

  5. Trait System and Pattern Matching: Rust's trait system provides a powerful mechanism for defining shared behavior across different types. Pattern matching allows for concise and readable code when working with complex data structures.

  6. Pattern Matching: Rust's pattern matching is expressive and powerful, enabling concise code for complex data manipulation and control flow. It helps in handling different cases and scenarios efficiently.

  7. Functional and Imperative Paradigms: Rust supports both functional and imperative programming styles. It allows you to choose the paradigm that best fits your problem while enforcing safety and performance guarantees.

  8. Crates and Package Management: Rust has a robust package management system with Cargo, simplifying project setup, dependency management, and building. Crates are Rust's version of libraries, which can be easily shared and distributed.

  9. No Null or Undefined Behavior: Rust eliminates null pointer dereferences and undefined behavior by enforcing strict rules for memory access. The Option and Result types are used to safely handle the absence of values and errors.

  10. Cross-Platform Compatibility: Rust's focus on portability and well-defined behavior makes it suitable for building cross-platform applications. It supports various operating systems and architectures.

  11. Ecosystem and Community: Rust has a rapidly growing and supportive community. The Rust ecosystem includes various libraries and tools covering various domains, from web development to embedded systems.

  12. Compiled Language: Rust is a compiled language, resulting in efficient and performant binaries. Its compile-time checks catch many errors before runtime, reducing the likelihood of bugs in production code.

These features collectively make Rust a strong choice for systems programming, embedded development, web services, and other applications where safety, performance, and concurrency are crucial.

Setting up Rust development environment

The Rust development environment is pretty easy to set up. The best way to install Rust is via Rust’s toolchain manager called rustup, available at https://rustup.rs. They laid down instructions on installing Rust there for your operating system.

Note: The Rust community has put up a set of teams and standards for managing Rust. One such standard is that all Rust-related websites use the .rs domain extension.

The installation you just did came with the following tools:

  • cargo: the Rust package manager.

  • rustc: The Rust compiler is sometimes invoked by cargo since your code is also treated as a package.

  • rustdoc: the Rust documentation tool for documenting comments in Rust code.

To check the version of these tools installed, run each of them with the --version flag:

rustc --version
cargo --version
rustdoc --version

Untitled (19).png

Let’s get familiar with each of these, starting with rustc.

Project Setup

Create a new project directory and create a file in it called hello.rs. In it, input this simple Hello World script:

fn main() {
	println!("Hello World!");
}

Now, run this command from the project directory:

rustc hello.rs

If you use ls command to check the content of the project directory, you should see a new file named hello. Run it like a bash script:

./hello
# Output is:
Hello World!

Great! You just used the Rust compiler to compile a Rust program into a binary file.

Untitled (20).png

Note: The Rust compiler is allegedly one of the smartest and most helpful compilers in the history of programming. The Rust community has reason to believe that if your code compiles without any errors, it is memory safe and almost bug-free.

Next, let’s see what cargo is. Delete this project folder you just created and run the following command:

cargo new hello

Next, change the directory into the src/ subdirectory and open the main.rs file. You should see the content:

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

Untitled (18).png

Great. You just used the Rust compilation manager to initialize a new binary project. Cargo is a very helpful tool that allows you to initialize, build, run, test, and release Rust projects and leverage existing Rust libraries in your project. There is an entire book to learn about Cargo, and its possibilities for your perusal.

Note: One of the greatest strengths promoting the adoption and evolution of the Rust programming language is the immersive documentation they provide for everything Rust-related. The libraries (called crates) available to Rust are available at Crates.io, the documentation for each crate is available at Docs.rs, jobs in the blockchain space are available at Rib.rs, and more.

Since we have not learned Rust, let’s pass on the rustdoc tool.

Next, run this command to compile and run the auto-generated program with Cargo:

cargo run

Great. You now know how to use Cargo to run a Rust program.

IDEs and tools

When writing Rust code, you will need all the help you can get. Install the Rust-Analyzer plugin from the extensions marketplace if you use Visual Studio Code. If you are a fan of the JetBrains ecosystem of IDEs, you should install the Rust plugin for your code and turn on the Cargo Check feature at the bottom of the window.

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board