← Rust EnglishChapter 01 of 13

Introduction to Rust

## Learning Objectives - Understand Rust history and philosophy - Set up Rust development environment - Write and run your first Rust program - Understand Rust syntax basics ## What is Rust? Rust is a systems programming language developed by Mozilla. It emphasizes safety, concurrency, and performance without requiring a garbage collector. ```rust fn main() { println!("Hello, World!"); } ``` ## Why Rust? ### Key Features - **Memory Safety** - Ownership system prevents memory errors - **Zero-Cost Abstractions** - High-level features compile to efficient code - **Fearless Concurrency** - Data race prevention at compile time - **Modern Tooling** - Cargo package manager, integrated testing ### Rust Philosophy 1. Memory safety through ownership 2. No runtime garbage collection 3. Fearless concurrency 4. Pattern matching and type inference ## Installing Rust ### Using rustup ```bash curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh ``` ### Verify Installation ```bash rustc --version cargo --version ``` ### Update Rust ```bash rustup update ``` ## Your First Program ### File: main.rs ```rust fn main() { println!("Hello, World!"); } ``` ### Compile and Run ```bash rustc main.rs # Creates executable 'main' ./main # Runs the program ``` Or with Cargo: ```bash cargo new hello_world cd hello_world cargo run ``` ## Cargo Commands | Command | Description | |---------|-------------| | cargo new | Create new project | | cargo build | Compile project | | cargo run | Build and run | | cargo test | Run tests | | cargo doc | Generate documentation | | cargo build --release | Release build | ## Rust Program Structure ```rust fn main() { // Entry point // Statements go here let message = "Hello"; // Immutable variable println!("{}", message); // Print line } ``` ### Components 1. **fn** - Function declaration keyword 2. **main** - Entry point function (no arguments, no return value) 3. **println!** - Macro for printing with newline 4. **Statements** - Commands ending with semicolon ## Comments ```rust // Single-line comment /* * Multi-line comment */ /// Documentation comment (for libraries) /// # Examples /// ``` /// let x = 5; /// ``` //! Inner doc comment (applies to enclosing item) ``` ## Printing and Formatting ```rust fn main() { // Simple print println!("Hello"); // Formatted print println!("Name: {}", "Alice"); println!("Age: {}", 30); println!("{} + {} = {}", 2, 3, 5); // Positional arguments println!("{0} is {1}, {0} is {2}", "Alice", 30, "human"); // Named arguments println!("{name} is {age} years old", name = "Bob", age = 25); // Debug trait println!("Debug: {:?}", (1, 2, 3)); // Alignment and precision println!("{:*<10}", "left"); // left-aligned, width 10 println!("{:*>10}", "right"); // right-aligned, width 10 println!("{:.*}", 3, 3.14159); // 3 decimal places } ``` ## Command Line Arguments ```rust use std::env; fn main() { let args: Vec = env::args().collect(); if args.len() > 1 { println!("Hello, {}!", args[1]); } else { println!("Hello, World!"); } } ``` ```bash cargo run Alice # Output: Hello, Alice! ``` ## IDE Support ### Recommended IDEs - **VS Code** with rust-analyzer extension - **IntelliJ Rust** (JetBrains) - **Vim/Neovim** with coc-rust-analyzer - **CLion** with Rust plugin ## Rust Editions ```bash rustc --version # Check installed version ``` | Edition | Year | Features | |---------|------|----------| | Rust 2015 | 2015 | Initial stable edition | | Rust 2018 | 2018 | Async/await, modules revamp | | Rust 2021 | 2021 | Closures, prelude changes | ## Hello World Project Structure ```text hello_world/ ├── Cargo.toml # Project manifest └── src/ └── main.rs # Source code ``` ### Cargo.toml ```toml [package] name = "hello_world" version = "0.1.0" edition = "2021" [dependencies] ``` ## Summary - Rust is a systems programming language focused on safety - Install via rustup: `curl https://sh.rustup.rs | sh` - `cargo new` creates new projects - `cargo run` builds and runs programs - `println!` is a macro for printing - Comments use `//` for single-line, `/* */` for multi-line

Comments

Comments powered by Giscus

To enable comments, add your Giscus embed code here.

Learn more about Giscus →