Getting Started with Rust

Rust is an immensely powerful tool that can be used to create almost any kind of software. It has a steep learning curve, but it's well worth mastering. Rust is performant, has excellent tools, and some of the best compiler help you'll ever experience.

Rust is available for Windows, Linux, and macOS and can generally be used to create software for any of these platforms. You may occasionally encounter libraries (called "crates" in the Rust world) that only work on one platform. For example, a crate might only be compiled for Linux but not Windows or macOS, which could prevent your project from running on those systems.

This article will help you get started with Rust.

Assumptions

  • A solid foundation of general computer skills
  • Comfort with the command line on your platform
  • Helpful (but not required): experience in another programming language
  • You're using Visual Studio Code as your editor

Key Concepts

Compiled vs. Interpreted Languages

Rust is a compiled language, meaning the programs you write need to be translated (compiled) into a format your computer can run. This is different from interpreted languages, where you can write code and see results immediately. For a more detailed explanation, check out this article on freeCodeCamp.

How Rust Organizes Code

Rust organizes code into packages and modules to keep things organized and control what's public or private. This helps manage larger projects and makes code reusable.

Cargo: Rust's Package Manager

Cargo is Rust's package management tool. It allows you to build, test, run, and deploy your programs. While you can write Rust code without Cargo, I don't recommend it. Cargo helps you organize your project and dependencies in a clean way and saves you a lot of typing.

You can write code in a file with an .rs extension and compile it with the rustc compiler, but this gets tedious quickly. Cargo makes the process much easier.

Installation

If you haven't already, follow the official installation instructions for your platform to get Rust installed.

Creating Your First Program

Let's create a simple program that says hello and displays your name. Create a directory in a location of your choosing and type:

cargo new HelloName

This creates a new binary project in a directory called HelloName.

What's a binary? A binary is a program you can run directly. (A library, by contrast, is code meant to be used by other programs or as a dependency in your own projects.)

Next, navigate into your project:

cd HelloName

Now open Visual Studio Code:

code .

Visual Studio Code will launch and show your project. You'll see a src folder—click on it to find main.rs, the entry point of your program. Cargo has already created a basic program for you:

// src/main.rs

fn main() {
     let name = "Jeff";
     println!("Hello, {}!", name);
}

Here's what's happening:

  • main() is the entry point—where your program starts
  • let name = "Jeff"; creates a variable called name and stores your name in it (change "Jeff" to your own name!)
  • println!("Hello, {}!", name); prints "Hello, [your name]!" to the console. The {} is replaced with the value of name

A couple of other things to note: Rust statements end with a semicolon, and code within a function is wrapped in curly braces {}.

To run your program, go back to the command line and type:

cargo run

You should see "Hello, [your name]!" printed to the terminal. Congratulations! You've just written your first Rust program.

What's Next?

You've taken your first step into Rust! The language has many concepts to explore—variables, functions, loops, and much more. Keep practicing, and you'll be building powerful programs in no time.

Resources