25:00
Focus
Sign in to save your learning paths. Guest paths may be lost if you clear your browser data.Sign in
Lesson 1

Running Your First Rust Program

~5 min50 XP

Introduction

Welcome to the world of systems programming! In this lesson, you will set up your development environment and build your very first Rust application using the industry-standard toolchain, ensuring you understand the mechanics behind compiling code safely and efficiently.

Installing the Rust Toolchain

To begin programming in Rust, you must first install the official toolchain installer, rustup. Rust is a language that prioritizes stability and safety, so the community has standardized on rustup to manage different versions of the compiler and associated tools. Unlike languages that rely on system-wide package managers, rustup allows you to manage installations locally, enabling you to switch between the stable, beta, and nightly builds of the compiler with ease.

On Unix-like systems (Linux or macOS), you can install the toolchain using the following command in your terminal: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Once installed, you should verify that your PATH is updated. This allows your shell to recognize the rustc compiler and the Cargo package manager. A common pitfall for beginners is failing to restart their terminal after installation; if your system cannot find the rustc command, simply open a new terminal window or run source $HOME/.cargo/env.

Remember: Rust is designed for memory safety without a garbage collector. By using rustup, you ensure that you are always using the correct library versions that protect against common exploits like buffer overflows.

Exercise 1Multiple Choice
Why is 'rustup' preferred over system package managers for installing Rust?

The Role of Cargo

While rustc is the direct compiler, in professional Rust development, you will almost exclusively use Cargo. Cargo is Rust’s build system and package manager. It handles everything from compiling your code and downloading external dependencies to running tests and generating documentation. When you create a new project, Cargo sets up a consistent file structure, which is crucial for maintaining project scalability.

When you run cargo new project_name, the tool creates a Cargo.toml file. This is the manifest file. It contains the project's metadata, such as the name, version, and a list of dependencies. Dependencies are listed under the [dependencies] section. Because Rust values explicit control, Cargo keeps track of exact library versions in a file called Cargo.lock. This ensures that if you share your project, other developers will compile your code using the exact same library versions you used.

Creating Your First Project

Now that you have the tools, let's create your first executable. Navigate to the directory where you want your project to live and run cargo new hello_world. This command creates a folder named hello_world containing a src directory and a Cargo.toml file.

Inside the src folder, you will find main.rs. This is the entry point of your application. Rust programs start by executing the main function. The standard structure looks like this:

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

Notice the exclamation mark after println!. This signifies that it is a macro, not a regular function. In Rust, macros are used for meta-programming, allowing the compiler to perform complex tasks like variable-argument parsing before the program actually runs.

Exercise 2True or False
In the expression 'println!()', the exclamation mark denotes that it is a standard function, not a macro.

Compiling and Executing

To run your program, navigate into the project directory and execute cargo run. Behind the scenes, Cargo performs two main steps: it compiles your source code into machine code using the Rust compiler (rustc) and then executes the resulting binary.

If you ever need to compile your code without running it (for example, to check for compilation errors), you can use cargo build. By default, this builds your project in debug mode. Debug mode prioritizes fast compilation times by including debugging information, which makes the binary larger and slower. When you are ready to distribute your software, you should run cargo build --release. This triggers optimizationsβ€”the compiler spends significantly more time analyzing your code to rearrange instructions, eliminate dead code, and inline functions, resulting in a much faster final binary.

Exercise 3Fill in the Blank
___ is the command used to compile a Rust project into an optimized release binary.
Exercise 4Multiple Choice
In the standard Rust project structure, which directory contains the 'main.rs' source file?

Key Takeaways

  • rustup is the essential tool for managing Rust's toolchain, allowing you to install and update the compiler safely.
  • Cargo acts as your project's command center, orchestrating the build process, managing dependencies, and ensuring reproducible builds through the manifest file.
  • The println! identifier is a macro, a powerful feature that allows for code expansion at compile time rather than runtime.
  • Use cargo build --release when preparing for production to enable compiler optimizations that ensure your systems-level code runs at peak performance.
Finding tutorial videos...
Go deeper
  • Why is managing toolchain versions locally important?πŸ”’
  • How does Cargo differ from the rustc compiler?πŸ”’
  • What is the difference between stable and nightly builds?πŸ”’
  • How does Rust ensure memory safety without garbage collection?πŸ”’
  • Can I use Rust if I don't have a Unix-based system?πŸ”’

Running Your First Rust Program β€” Rust programming | crescu