Published on

Chapter 1: Getting Started with Rust

8 min read

Authors
banner

Chapter 1: Getting Started with Rust

Welcome to the world of Rust programming! This comprehensive guide will take you through everything you need to know to get started with Rust, from installation to writing your first programs using Cargo. Whether you're coming from other programming languages or just starting your programming journey, this guide will set you up for success.

What You'll Learn

In this guide, we'll cover:

  • Installing Rust on Linux, macOS, and Windows
  • Writing a program that prints "Hello, world!"
  • Using Cargo, Rust's package manager and build system
  • Understanding the anatomy of Rust programs
  • Working with Rust's development tools

Installing Rust

The first step in your Rust journey is getting the language installed on your system. We'll use rustup, the official Rust toolchain installer that manages Rust versions and associated tools.

What is rustup?

rustup is a command-line tool for managing Rust versions and associated tools. It ensures you always have access to the latest stable version of Rust and makes it easy to switch between different versions when needed.

Command Line Notation

Throughout this guide, we'll show terminal commands. Here's what the symbols mean:

  • Lines starting with $ are commands for Linux/macOS/PowerShell
  • Lines starting with > are commands for Windows CMD
  • Lines without these symbols show command output

Installing on Linux or macOS

Open a terminal and run this command:

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

This downloads and runs the rustup installation script. If successful, you'll see:

Rust is installed now. Great!

Additional Requirements

You'll also need a linker (a program that joins compiled outputs into one file). Most systems already have one, but if you encounter linker errors:

On macOS:

$ xcode-select --install

On Linux (Ubuntu example):

$ sudo apt install build-essential

Installing on Windows

  1. Visit https://www.rust-lang.org/tools/install
  2. Follow the installation instructions
  3. When prompted, install Visual Studio (provides the linker and native libraries)

Verifying Installation

Check if Rust installed correctly:

$ rustc --version

You should see output like:

rustc 1.70.0 (90c541806 2023-05-31)

If this doesn't work, ensure Rust is in your system PATH:

Windows CMD:

> echo %PATH%

PowerShell:

> echo $env:Path

Linux/macOS:

$ echo $PATH

Keeping Rust Updated

Update to the latest version:

$ rustup update

Uninstall Rust and rustup:

$ rustup self uninstall

Local Documentation

Rust includes offline documentation:

$ rustup doc

This opens comprehensive API documentation in your browser - invaluable when you're unsure about types or functions!

Your First Rust Program: Hello, World!

Now let's write the traditional first program that every programmer writes when learning a new language.

Setting Up Your Project Directory

Create a workspace for your Rust projects:

Linux/macOS/PowerShell:

$ mkdir ~/projects
$ cd ~/projects
$ mkdir hello_world
$ cd hello_world

Windows CMD:

> mkdir "%USERPROFILE%\projects"
> cd /d "%USERPROFILE%\projects"
> mkdir hello_world
> cd hello_world

Writing Your First Program

Create a file called main.rs (Rust files always end with .rs):

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

Compiling and Running

Compile your program:

$ rustc main.rs

Run the executable:

$ ./main          # Linux/macOS
$ .\main.exe      # Windows

Output:

Hello, world!

Congratulations! You're now officially a Rust programmer! 🎉

Understanding the Anatomy of a Rust Program

Let's break down what each part of our program does:

The Main Function

fn main() {
    // Function body goes here
}

Key concepts:

  • fn declares a function
  • main is special - it's the entry point of every executable Rust program
  • () indicates no parameters
  • {} wraps the function body (required for all functions)

The println! Macro

println!("Hello, world!");

Important details:

  1. println! is a macro, not a function (notice the !)

    • Macros can do things functions can't
    • We'll explore macros in detail later
  2. "Hello, world!" is a string literal

    • Passed as an argument to the macro
    • Gets printed to the screen
  3. The semicolon (;) ends the statement

    • Most lines in Rust end with semicolons
    • Indicates the expression is complete

Compilation Process

Rust is an ahead-of-time compiled language, meaning:

  1. Compile first: rustc main.rs creates an executable
  2. Run separately: ./main executes the program
  3. Standalone executable: Others can run it without installing Rust

This differs from interpreted languages like Python or JavaScript, where you need the interpreter installed to run programs.

After compilation, you'll see:

  • main.rs - your source code
  • main (or main.exe on Windows) - the executable
  • main.pdb (Windows only) - debugging information

Hello, Cargo!

While rustc works fine for simple programs, real-world Rust development uses Cargo - Rust's build system and package manager.

What is Cargo?

Cargo handles many tasks for you:

  • Building your code
  • Downloading dependencies (external libraries)
  • Building those dependencies
  • Managing project structure

Verify Cargo Installation

Cargo comes with Rust, but let's verify:

$ cargo --version

Creating a Cargo Project

Create a new project:

$ cargo new hello_cargo
$ cd hello_cargo

This creates:

hello_cargo/
├── Cargo.toml
├── src/
│   └── main.rs
└── .gitignore

Understanding Cargo.toml

Cargo.toml is your project's configuration file (TOML = Tom's Obvious, Minimal Language):

[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2024"

[dependencies]

Sections explained:

  • [package] - metadata about your package
  • name - project name
  • version - current version (follows semantic versioning)
  • edition - Rust edition to use
  • [dependencies] - external crates your project needs

Project Structure

Key conventions:

  • Source code lives in src/
  • main.rs is the main binary entry point
  • Cargo.toml stays in the project root
  • Other files (README, license, etc.) go in the root

Building and Running with Cargo

Build Your Project

$ cargo build

Output:

Compiling hello_cargo v0.1.0 (file:///projects/hello_cargo)
Finished dev [unoptimized + debuginfo] target(s) in 2.85 secs

This creates an executable at target/debug/hello_cargo.

Run the Executable

$ ./target/debug/hello_cargo

Build and Run in One Step

$ cargo run

This is more convenient and what most developers use daily.

Check Without Building

$ cargo check

Why use cargo check?

  • Much faster than cargo build
  • Verifies your code compiles without creating an executable
  • Perfect for quick feedback while writing code

Release Builds

For production-ready builds:

$ cargo build --release

Differences:

  • Creates executable in target/release/
  • Includes optimizations for better performance
  • Takes longer to compile
  • Use this for benchmarking and final releases

Key Cargo Commands Summary

CommandPurpose
cargo new <name>Create new project
cargo buildCompile project (debug mode)
cargo runCompile and run
cargo checkCheck compilation without building
cargo build --releaseCompile with optimizations

Working with Existing Projects

To work on existing Rust projects:

$ git clone example.org/someproject
$ cd someproject
$ cargo build

Development Environment Setup

Text Editors and IDEs

Rust works with any text editor, but many have built-in Rust support:

  • VS Code with rust-analyzer extension
  • IntelliJ IDEA with Rust plugin
  • Vim/Neovim with rust.vim
  • Emacs with rust-mode

The Rust team focuses on rust-analyzer for IDE support.

Formatting Your Code

Rust includes rustfmt for automatic code formatting:

$ rustfmt main.rs

This ensures consistent style across all Rust projects.

Working Offline

For offline development, pre-download common dependencies:

$ cargo new get-dependencies
$ cd get-dependencies
$ cargo add rand@0.8.5 trpl@0.2.0

Then use the --offline flag with cargo commands.

Key Concepts and Keywords

Essential Rust Concepts from This Guide

  1. Functions (fn)

    • Entry point: main() function
    • Curly braces {} are required
    • Parameters go in parentheses ()
  2. Macros

    • Identified by ! (e.g., println!)
    • More powerful than functions
    • Can generate code at compile time
  3. Statements vs Expressions

    • Most lines end with semicolons ;
    • Semicolons indicate statement completion
  4. Project Management

    • rustc for direct compilation
    • cargo for project management
    • Cargo.toml for configuration

Important Keywords and Terms

  • fn - Function declaration keyword
  • main - Entry point function name
  • println! - Print macro with newline
  • Crate - Rust package/library
  • Edition - Rust language version (2018, 2021, 2024)
  • rustup - Rust toolchain installer
  • rustc - Rust compiler
  • cargo - Build tool and package manager

Best Practices and Tips

Project Organization

  • Always use Cargo for new projects
  • Keep source code in src/
  • Use descriptive project names with underscores
  • File names should use underscores (e.g., hello_world.rs)

Development Workflow

  1. Use cargo check frequently while coding
  2. Use cargo run for testing
  3. Use cargo build --release for final builds
  4. Keep documentation handy with rustup doc

Common Beginner Mistakes

  • Forgetting the ! in println!
  • Missing semicolons
  • Not using Cargo for projects
  • Forgetting to install a linker

What's Next?

You've successfully:

  • Installed Rust using rustup
  • Written and run a "Hello, world!" program
  • Created a project using Cargo
  • Learned essential Rust concepts and tools
  1. Build a guessing game (Chapter 2 of the Rust Book)
  2. Learn common programming concepts in Rust (Chapter 3)
  3. Explore the standard library using rustup doc
  4. Practice with small projects using Cargo

Additional Resources

Welcome to the Rust community! You're now equipped with the fundamental tools and knowledge to start your Rust programming journey. The language has a reputation for being challenging but rewarding, and with these basics mastered, you're well on your way to writing safe, fast, and reliable code.

Happy coding! 🦀

© 2025 President-XD