No history yet

Introduction to Rust

Meet Rust

Rust is a modern programming language focused on three things: performance, safety, and concurrency. Think of it as having the raw speed of languages like C++, but with guardrails that prevent common and frustrating bugs before your code even runs.

Rust is a systems programming language that focuses on safety, concurrency, and performance.

What does this mean in practice? Rust's compiler is famously strict. It checks your code for issues related to how your program uses memory. This prevents entire categories of errors, like null pointer dereferences and data races, which are notorious problems in other systems languages. The benefit is incredibly reliable and efficient software. This makes Rust a great choice for everything from web servers and command-line tools to embedded devices.

Setting Up Your Environment

Getting started with Rust is straightforward. The official tool for managing Rust versions and associated tools is called rustup. It installs everything you need to write, compile, and manage Rust projects.

To install Rust, open your terminal and run the following command. It will guide you through the installation process.

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

Once installed, rustup gives you a few key command-line tools:

  • rustc: The Rust compiler. You can use it to compile .rs files directly.
  • cargo: Rust's build system and package manager. This is the tool you'll use most of the time to build, test, and manage your projects and their dependencies.
  • rustdoc: A tool for building documentation for your code.

After installation, close and reopen your terminal. Then, verify that everything is working by checking the version.

rustc --version

You should see the version number, commit hash, and commit date of the latest stable version of Rust that was installed.

Your First Rust Program

While you can use rustc to compile a single file, the standard way to create a Rust project is with Cargo. Let's create a new project called hello_world.

cargo new hello_world

Cargo generates a simple project structure for you. Inside the hello_world directory, you'll find a Cargo.toml file, which is a configuration file for your project. You'll also find a src directory containing a file named main.rs.

Open src/main.rs. It contains a simple "Hello, world!" program.

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

Let's break this down.

fn main() defines the main function. The fn keyword is used to declare a new function, and main is the special name for the function that serves as the entry point for every executable Rust program.

The line println!("Hello, world!"); does the printing. You might notice the exclamation mark. This means we're calling a Rust macro, not a normal function. We'll explore the difference later, but for now, just know that macros offer more advanced capabilities than functions. Finally, the line ends with a semicolon (;), which marks the end of the statement.

Compiling and Running

Now, let's compile and run our program. Navigate into your project directory (hello_world) in your terminal. Cargo makes this process very simple.

To compile your program, run:

cargo build

This command creates an executable file in target/debug/hello_world (or target\debug\hello_world on Windows). You can run this executable directly, but there's an easier way.

To compile and run your program in one step, use:

cargo run

You should see Hello, world! printed to your terminal. There's one more useful command to know: cargo check. This command quickly checks your code to make sure it compiles but doesn't produce an executable. It's much faster than a full build and is great for checking for errors as you write code.

Use cargo new to start projects, cargo run to execute them, and cargo check to quickly look for errors.

Now you've got the essentials down. Let's test your knowledge.

Quiz Questions 1/5

What are the three primary goals that the Rust programming language is designed to prioritize?

Quiz Questions 2/5

Which command-line tool is Rust's build system and package manager, used most often to create, build, and manage projects?

That covers the absolute basics of getting started with Rust. You've set up your environment and learned how to use Cargo to create, build, and run a project.