Lesson 3 of 50 · rust
Your First Program (Hello World)
Duration: 10 minutes
Lesson 3 of 50 · rust
Duration: 10 minutes
rust\nfn main() {\n println!(\"Hello, world!\");\n}\n\n\n## Breaking It Down\n\n### The main Function\n\nWhere every executable Rust program starts:\n\nrust\nfn main() {\n // Program execution begins here\n}\n\n\n- fn declares a function\n- main is special—automatically called when the program starts\n- Functions without explicit return types implicitly return () (unit type)\n\n### The println! Macro\n\nprintln! is a macro (note the !), not a regular function. Macros are more powerful:\n\nrust\nprintln!(\"Hello\"); // Simple print\nprintln!(\"Hello, {}\", name); // String interpolation\nprintln!(\"x = {}, y = {}\", x, y); // Multiple arguments\nprintln!(\"Debug: {:?}\", var); // Debug formatting\n\n\nMacros are identified by ! and expanded at compile time.\n\n## Running Your Program\n\nbash\ncargo run\n\n\nOutput:\n\nHello, world!\n\n\n## Exploring println!\n\nrust\nfn main() {\n // Basic output\n println!(\"Welcome to Rust!\");\n \n // Variables in output\n let name = \"Alice\";\n let age = 30;\n println!(\"Name: {}, Age: {}\", name, age);\n \n // Debug output\n let values = vec![1, 2, 3];\n println!(\"Values: {:?}\", values);\n \n // Empty line\n println!();\n \n // Expressions\n println!(\"2 + 2 = {}\", 2 + 2);\n}\n\n\n## Important Details\n\nSemicolons: The semicolon matters in Rust:\n\nrust\nprintln!(\"Hello\"); // Statement—no return value\nprintln!(\"Hello\") // Returns unit type (rarely used)\n\n\nBorrowing: println! borrows values rather than taking ownership, so you can use them afterwards.\n\n## Common Mistakes\n\n1. Forgetting parentheses: println!\"Hello\"; won't compile\n2. Wrong argument count: Missing arguments for placeholders\n3. Unescaped quotes: Inside strings, use \\\" for quotes\n\n## What's Next\n\nNow you understand the program structure: a main function that executes first and println! for output. You're ready for variables, functions, and control flow.