- Published on
Chapter 1: Getting Started with Rust
8 min read
- Authors
- Name
- Mohsin Mukhtiar
- @justmohsin_

Table of Contents
- Chapter 1: Getting Started with Rust
- What You'll Learn
- Installing Rust
- What is rustup?
- Command Line Notation
- Installing on Linux or macOS
- Additional Requirements
- Installing on Windows
- Verifying Installation
- Keeping Rust Updated
- Local Documentation
- Your First Rust Program: Hello, World!
- Setting Up Your Project Directory
- Writing Your First Program
- Compiling and Running
- Understanding the Anatomy of a Rust Program
- The Main Function
- The println! Macro
- Compilation Process
- Hello, Cargo!
- What is Cargo?
- Verify Cargo Installation
- Creating a Cargo Project
- Understanding Cargo.toml
- Project Structure
- Building and Running with Cargo
- Build Your Project
- Run the Executable
- Build and Run in One Step
- Check Without Building
- Release Builds
- Key Cargo Commands Summary
- Working with Existing Projects
- Development Environment Setup
- Text Editors and IDEs
- Formatting Your Code
- Working Offline
- Key Concepts and Keywords
- Essential Rust Concepts from This Guide
- Important Keywords and Terms
- Best Practices and Tips
- Project Organization
- Development Workflow
- Common Beginner Mistakes
- What's Next?
- Recommended Next Steps
- Additional Resources
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
- Visit https://www.rust-lang.org/tools/install
- Follow the installation instructions
- 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 functionmain
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:
println!
is a macro, not a function (notice the!
)- Macros can do things functions can't
- We'll explore macros in detail later
"Hello, world!" is a string literal
- Passed as an argument to the macro
- Gets printed to the screen
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:
- Compile first:
rustc main.rs
creates an executable - Run separately:
./main
executes the program - 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 codemain
(ormain.exe
on Windows) - the executablemain.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 packagename
- project nameversion
- 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 pointCargo.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
Command | Purpose |
---|---|
cargo new <name> | Create new project |
cargo build | Compile project (debug mode) |
cargo run | Compile and run |
cargo check | Check compilation without building |
cargo build --release | Compile 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
Functions (
fn
)- Entry point:
main()
function - Curly braces
{}
are required - Parameters go in parentheses
()
- Entry point:
Macros
- Identified by
!
(e.g.,println!
) - More powerful than functions
- Can generate code at compile time
- Identified by
Statements vs Expressions
- Most lines end with semicolons
;
- Semicolons indicate statement completion
- Most lines end with semicolons
Project Management
rustc
for direct compilationcargo
for project managementCargo.toml
for configuration
Important Keywords and Terms
fn
- Function declaration keywordmain
- Entry point function nameprintln!
- 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
- Use
cargo check
frequently while coding - Use
cargo run
for testing - Use
cargo build --release
for final builds - Keep documentation handy with
rustup doc
Common Beginner Mistakes
- Forgetting the
!
inprintln!
- 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
Recommended Next Steps
- Build a guessing game (Chapter 2 of the Rust Book)
- Learn common programming concepts in Rust (Chapter 3)
- Explore the standard library using
rustup doc
- 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! 🦀