Beginner Level

Getting Started

Chapter 1: Getting Started with Rust šŸš€

Welcome to your Rust programming journey! In this chapter, you'll learn what Rust is, why it's special, and how to set up your development environment.

What is Rust? šŸ¦€

Rust is a systems programming language that focuses on:

  • Memory Safety: No null pointer dereferences, buffer overflows, or memory leaks
  • Performance: Zero-cost abstractions and minimal runtime overhead
  • Concurrency: Safe concurrent programming without data races
  • Reliability: Catch bugs at compile time, not runtime

Why Learn Rust?

  1. Growing Industry Adoption: Used by companies like Mozilla, Dropbox, Discord, and Microsoft
  2. Performance: As fast as C/C++ but much safer
  3. Career Opportunities: High demand for Rust developers
  4. Future-Proof: Designed for modern computing challenges

Installing Rust

Method 1: Using Rustup (Recommended)

  1. Visit rustup.rs
  2. Run the installation command for your OS:

Windows:

# Run in PowerShell
Invoke-WebRequest -Uri "https://win.rustup.rs/" -OutFile "rustup-init.exe"
.\rustup-init.exe

macOS/Linux:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  1. Restart your terminal or run:
source ~/.cargo/env

Verify Installation

# Check Rust version
rustc --version

# Check Cargo (Rust's package manager) version
cargo --version

You should see output like:

rustc 1.70.0 (90c541806 2023-05-31)
cargo 1.70.0 (ec8a8a0ca 2023-04-25)

Your First Rust Program

Let's write the traditional "Hello, World!" program:

Step 1: Create a new project

cargo new hello_world
cd hello_world

Step 2: Examine the project structure

hello_world/
ā”œā”€ā”€ Cargo.toml    # Project configuration
└── src/
    └── main.rs   # Your code goes here

Step 3: Look at the generated code

Open src/main.rs:

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

Step 4: Run the program

cargo run

Output:

   Compiling hello_world v0.1.0 (/path/to/hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 0.50s
     Running `target/debug/hello_world`
Hello, world!

Understanding the Code

Let's break down our first Rust program:

fn main() {                    // Function declaration
    println!("Hello, world!"); // Macro call (note the !)
}
  • fn main(): The entry point of every Rust program
  • println!: A macro (not a function) that prints text to the console
  • The ! indicates it's a macro, not a regular function
  • Statements end with semicolons (;)

Cargo: Rust's Package Manager

Cargo is Rust's build system and package manager. Key commands:

cargo new project_name    # Create new project
cargo build              # Compile the project
cargo run                # Compile and run
cargo check              # Check if code compiles (faster than build)
cargo test               # Run tests
cargo doc                # Generate documentation

Development Environment Setup

Recommended Editors/IDEs:

  1. VS Code with rust-analyzer extension
  2. IntelliJ IDEA with Rust plugin
  3. Vim/Neovim with rust.vim
  4. Emacs with rust-mode

Essential Tools:

# Install useful tools
cargo install cargo-watch    # Auto-rebuild on file changes
cargo install cargo-edit     # Add/remove dependencies easily
rustup component add clippy  # Rust linter
rustup component add rustfmt # Code formatter

Practice Exercises

Exercise 1: Modify Hello World

Change the "Hello, world!" message to greet yourself by name.

Exercise 2: Multiple Prints

Create a program that prints:

Welcome to Rust!
This is my first program.
Let's learn together!

Exercise 3: Using cargo watch

Install cargo-watch and use it to automatically run your program when files change:

cargo install cargo-watch
cargo watch -x run

Common Beginner Mistakes

  1. Forgetting semicolons: Most statements need them
  2. Case sensitivity: Rust is case-sensitive (Main ≠ main)
  3. Using print instead of println!: Use println! for newlines

What's Next?

In the next chapter, we'll explore variables and data types - the building blocks of any program!

Key Takeaways

  • āœ… Rust prioritizes safety, performance, and concurrency
  • āœ… Use rustup to install and manage Rust
  • āœ… cargo is your best friend for project management
  • āœ… Every Rust program starts with a main() function
  • āœ… println! is a macro for printing to console

Ready for Chapter 2? → Variables and Data Types

šŸ¦€ Rust Programming Tutorial

Learn from Zero to Advanced

Built with Next.js and Tailwind CSS • Open Source