A comprehensive, hands-on guide to learning Rust programming language. Perfect for experienced developers from PHP, Node.js, and other backgrounds who want to learn Rust through practical, runnable examples.
This repository is designed for:
- Backend developers with experience in PHP, Node.js, Python, etc.
- Developers with 10+ years of programming experience
- Anyone who wants to learn Rust through practical examples
- Those who prefer learning by running and modifying code
Make sure you have Rust installed. If not, install it from rustup.rs:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh# Clone the repository
git clone https://github.com/motyar/rust.git
cd rust
# Run any example
cargo run -p basics
cargo run -p control_flow
cargo run -p ownership
# ... and so onThe examples are organized in a progressive learning path. Follow them in order:
Learn fundamental Rust concepts:
- Variables and mutability
- Data types (integers, floats, booleans, chars)
- Tuples and arrays
- Functions
- Comments and constants
cargo run -p basicsMaster program flow control:
- If/else statements
- Loops (loop, while, for)
- Match expressions (like switch, but better!)
- Pattern matching basics
cargo run -p control_flowUnderstand Rust's unique ownership system:
- Ownership rules
- Move semantics
- References and borrowing
- Mutable references
- Slices
cargo run -p ownershipCreate custom data types:
- Defining and using structs
- Tuple structs
- Methods and associated functions
- Enums and pattern matching
- Option and Result types
cargo run -p structs_enumsWork with common collections:
- Vectors (dynamic arrays)
- Strings (UTF-8 encoded)
- HashMaps (key-value pairs)
- Iterating and transforming collections
cargo run -p collectionsHandle errors the Rust way:
- panic! for unrecoverable errors
- Result<T, E> for recoverable errors
- The ? operator
- Custom error types
- Error propagation
cargo run -p error_handlingWrite reusable, flexible code:
- Generic functions and structs
- Trait definitions and implementations
- Trait bounds
- Default implementations
- Common traits (Debug, Clone, Copy, etc.)
cargo run -p generics_traitsOrganize your code:
- Module system
- Paths and use keyword
- Public vs private
- Separating code into files
cargo run -p modulesWrite reliable code with tests:
- Unit tests
- Integration tests
- Test organization
- Assertions
- Test attributes
# Run the example
cargo run -p testing
# Run the tests
cargo test -p testingRead and write files:
- Reading files
- Writing files
- Appending to files
- Command line arguments
- Standard input
cargo run -p file_io
# Try with arguments
cargo run -p file_io -- arg1 arg2 arg3Write concurrent programs safely:
- Creating threads
- Message passing with channels
- Shared state with Mutex
- Arc for shared ownership
- Thread safety guarantees
cargo run -p concurrencyLearn Rust idioms and patterns:
- Closures (anonymous functions)
- Iterators and adapters
- Smart pointers (Box, Rc, RefCell)
- Advanced pattern matching
- If let and while let
cargo run -p patternscargo run -p <package-name>cargo build --all# Run all tests
cargo test --all
# Run tests for specific package
cargo test -p testingcargo check --all- The Rust Book - Official comprehensive guide
- Rust by Example - Learn by examples
- Rust Standard Library - API documentation
- Rustlings - Small exercises
- Exercism Rust Track - Coding exercises
- Run Every Example: Don't just readβrun the code and see the output
- Modify the Code: Change values, add features, break things and fix them
- Read Compiler Errors: Rust's compiler is very helpfulβread the error messages carefully
- Follow the Order: The examples build on each other
- Take Your Time: Especially with ownership and borrowing
- Practice: Write your own small programs after each section
Coming from PHP, Node.js, or other languages, here are Rust's key differences:
- No Garbage Collector: Memory is managed through ownership
- Compile-Time Safety: Most bugs are caught before running
- Zero-Cost Abstractions: High-level features with no runtime cost
- Immutable by Default: Variables are immutable unless marked
mut - Fearless Concurrency: Data races are impossible at compile time
- No Null: Use
Option<T>instead - No Exceptions: Use
Result<T, E>for error handling
After completing these examples, you can:
- Build a CLI tool with clap
- Create a web server with Actix or Rocket
- Build a REST API
- Learn async programming with Tokio
- Explore awesome-rust for more libraries
Found an issue or want to add more examples? Feel free to:
- Open an issue
- Submit a pull request
- Suggest improvements
This repository is for educational purposes. Feel free to use, modify, and share!
Happy Learning! π¦
Remember: Rust has a steep learning curve, but it's worth it. Take your time, practice, and don't get discouraged by compiler errorsβthey're here to help you write better code!