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?
- Growing Industry Adoption: Used by companies like Mozilla, Dropbox, Discord, and Microsoft
- Performance: As fast as C/C++ but much safer
- Career Opportunities: High demand for Rust developers
- Future-Proof: Designed for modern computing challenges
Installing Rust
Method 1: Using Rustup (Recommended)
- Visit rustup.rs
- Run the installation command for your OS:
Windows:
# Run in PowerShell
Invoke-WebRequest -Uri "https://win.rustup.rs/" -OutFile "rustup-init.exe"
.\rustup-init.exemacOS/Linux:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh- Restart your terminal or run:
source ~/.cargo/envVerify Installation
# Check Rust version
rustc --version
# Check Cargo (Rust's package manager) version
cargo --versionYou 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_worldStep 2: Examine the project structure
hello_world/
āāā Cargo.toml # Project configuration
āāā src/
āāā main.rs # Your code goes hereStep 3: Look at the generated code
Open src/main.rs:
fn main() {
println!("Hello, world!");
}Step 4: Run the program
cargo runOutput:
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 programprintln!: 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 documentationDevelopment Environment Setup
Recommended Editors/IDEs:
- VS Code with rust-analyzer extension
- IntelliJ IDEA with Rust plugin
- Vim/Neovim with rust.vim
- 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 formatterPractice 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 runCommon Beginner Mistakes
- Forgetting semicolons: Most statements need them
- Case sensitivity: Rust is case-sensitive (
Maināmain) - Using
printinstead ofprintln!: Useprintln!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
rustupto install and manage Rust - ā
cargois 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