HOW-TO: Getting Started with Rust Programming
Beginner's guide to writing your first Rust programs using both rustc and cargo. Learn the anatomy of Rust code, compilation process, and best practices for starting your Rust journey.
HOW-TO: Getting Started with Rust Programming
Overview
Now that you have Rust installed (see Howto Install Rust Linux for installation), it's time to write your first programs. This guide walks you through your first Rust program, explains how the language works, and introduces you to Rust's tooling.
What you'll learn:
- How to write and compile a simple Rust program
- The anatomy of a Rust program
- Compilation vs execution
- When to use
rustcvscargo - Rust coding conventions
Before You Start
Prerequisites
- Rust installed via rustup (see Howto Install Rust Linux)
- A terminal and text editor
- Basic familiarity with the command line
Verify Your Setup
rustc --version
cargo --version
rustup --version
All three commands should work. If not, go back to the installation guide.
Your First Program: Hello, World!
Step 1: Create a Project Directory
# Create a projects directory in your home folder
mkdir -p ~/projects
cd ~/projects
# Create a directory for your first program
mkdir hello_world
cd hello_world
Step 2: Create the Source File
Create a file named main.rs:
# Using a text editor
nano main.rs
# or vim, VS Code, etc.
Step 3: Write Your First Program
Type the following code into main.rs:
fn main() {
println!("Hello, world!");
}
Save the file.
Step 4: Compile the Program
rustc main.rs
This creates an executable file. You'll see:
main(on Linux/macOS)main.exe(on Windows)
Step 5: Run It
On Linux/macOS:
./main
On Windows:
.\main
Expected output:
Hello, world!
Congratulations! You've written and run your first Rust program. š
The Anatomy of a Rust Program
Let's break down what you just wrote:
The main Function
fn main() {
// Function body
}
Key points:
fndeclares a functionmainis special ā it's the entry point of every Rust program()means the function takes no parameters{}wraps the function body (always required in Rust)- Rust style: opening brace
{goes on the same line as the function name
The println! Macro
println!("Hello, world!");
Three important details:
-
The exclamation mark (
!) indicates this is a macro, not a regular function- Macros generate code to extend Rust's syntax
- You'll learn more about macros later
- For now:
!means "macro", not function
-
The string argument
"Hello, world!"- Passed to
println!to print to the screen - Strings are enclosed in double quotes
- Passed to
-
The semicolon (
;) ends the statement- Most Rust code lines end with semicolon
- Semicolon means "this expression is complete, move to the next"
Compilation vs Execution
Rust is an ahead-of-time compiled language, which differs from interpreted languages:
| Language Type | Example | Process |
|---|---|---|
| Compiled | Rust, C, C++ | Source ā Compile ā Binary ā Run |
| Interpreted | Python, JavaScript, Ruby | Source ā Run (with interpreter) |
Rust Compilation Process
# Step 1: Compile
rustc main.rs
# Creates: main (or main.exe on Windows)
# Step 2: Run the executable
./main
# Output: Hello, world!
Benefits of Compilation
- ā Fast execution (no interpreter overhead)
- ā Share the binary with others (they don't need Rust installed)
- ā Early error detection (compiler catches bugs before runtime)
- ā Optimizations (compiler makes smart decisions about code)
Drawbacks
- ā Slower development loop (edit ā compile ā run)
- ā Larger file size (binary includes all dependencies)
Solution: Use cargo for development (we'll cover this next).
Naming Conventions
File Names
# Single word
main.rs # ā
Good
parser.rs # ā
Good
# Multiple words: use underscores
hello_world.rs # ā
Good
file_parser.rs # ā
Good
helloworld.rs # ā Avoid
fileparser.rs # ā Avoid
Variable and Function Names
// Functions and variables: snake_case
fn main() { } // ā
Good
fn calculate_sum() { } // ā
Good
fn Calculate_Sum() { } // ā Avoid
let user_name = "Alice"; // ā
Good
let user_name_2 = "Bob"; // ā
Good
let userName = "Carol"; // ā Avoid
Constants and Type Names
// Constants: SCREAMING_SNAKE_CASE
const MAX_USERS: i32 = 100; // ā
Good
const DEFAULT_TIMEOUT: u64 = 30; // ā
Good
// Types and Structs: PascalCase
struct User { } // ā
Good
enum Direction { } // ā
Good
impl Rectangle { } // ā
Good
struct user { } // ā Avoid
enum direction { } // ā Avoid
Your Second Program: Variables and Printing
Let's write a slightly more complex program. Create variables.rs:
fn main() {
let name = "Rust";
let version = 2026;
let compiled = true;
println!("Language: {}", name);
println!("Year: {}", version);
println!("Compiled: {}", compiled);
println!("About {} (v{}): {}", name, version, compiled);
}
Compile and Run
rustc variables.rs
./variables
Expected output:
Language: Rust
Year: 2026
Compiled: true
About Rust (v2026): true
What's Happening
letdeclares a variableprintln!can take multiple arguments{}is a placeholder for values (we'll learn about formatting later)- Variables are immutable by default
From rustc to cargo
When to Use rustc
rustc is fine for tiny, one-file programs:
rustc hello.rs
./hello
Use rustc when:
- Learning the basics
- Writing single-file programs
- Testing quick ideas
When to Use cargo
cargo is Rust's package manager and build tool. Use it for real projects:
# Create a new project
cargo new my_app
cd my_app
# Run the project
cargo run
# Build for release
cargo build --release
Use cargo when:
- Building projects with multiple files
- Managing dependencies
- Sharing code with others
- Working on anything beyond a one-file program
Cargo Project Structure
my_app/
āāā Cargo.toml # Project metadata and dependencies
āāā src/
ā āāā main.rs # Your code
āāā target/
āāā debug/ # Compiled output
We'll cover cargo in detail in a future guide.
Common Beginner Mistakes
Missing Semicolon
// ā Wrong
fn main() {
println!("Hello") // Missing semicolon!
}
// ā
Right
fn main() {
println!("Hello");
}
Error: expected ';'
Mismatched Braces
// ā Wrong
fn main() {
println!("Hello")
// Missing closing brace
// ā
Right
fn main() {
println!("Hello");
}
Error: mismatched closing delimiter
Wrong Macro Name
// ā Wrong
println("Hello"); // Missing ! and missing argument quotes
// ā
Right
println!("Hello"); // ! indicates macro
Error: cannot find function 'println' in this scope
Forgetting the main Function
// ā Wrong
fn say_hello() {
println!("Hello!");
}
// ā
Right
fn main() {
println!("Hello!");
}
Error: error: could not compile 'hello_world' (bin target 'hello_world')
Code Formatting with rustfmt
Rust has a built-in code formatter:
# Format your code automatically
rustfmt main.rs
# Check without modifying
rustfmt --check main.rs
This enforces consistent style across all Rust code. No more debates about spacing!
Running Code Inline (Rust Playground)
For quick testing without creating files, use the Rust Playground:
Online: https://play.rust-lang.org/
Features:
- Write and run Rust code in your browser
- No installation needed
- Share code with others via URL
- Perfect for learning and testing
Useful Compiler Errors
Helpful Error Messages
Rust's compiler is famous for its helpful error messages. Here's an example:
fn main() {
let x = 5;
x = 6; // Error: can't reassign immutable variable
}
Compiler output:
error[E0384]: cannot assign twice to immutable variable `x`
--> src/main.rs:3:5
|
2 | let x = 5;
| - first assignment to `x`
3 | x = 6;
| ^^^^^ cannot assign twice to immutable variable
|
help: consider making this binding mutable
|
2 | let mut x = 5;
| +++
error: could not compile `example` (bin target 'example')
Notice: The compiler doesn't just tell you there's an error. It:
- Shows exactly where the error is
- Explains what went wrong
- Suggests how to fix it
This is why Rust developers love the compiler ā it helps you learn!
Next Steps
1. Learn About Variables and Types
fn main() {
let x: i32 = 5; // integer
let y: f64 = 3.14; // floating-point
let is_rust_great: bool = true; // boolean
}
2. Use Control Flow
fn main() {
let number = 6;
if number % 4 == 0 {
println!("Divisible by 4");
} else if number % 2 == 0 {
println!("Divisible by 2");
} else {
println!("Not divisible");
}
}
3. Write Functions
fn add(a: i32, b: i32) -> i32 {
a + b
}
fn main() {
let result = add(5, 3);
println!("Result: {}", result);
}
4. Work with Collections
fn main() {
let numbers = vec![1, 2, 3, 4, 5];
for num in numbers {
println!("{}", num);
}
}
5. Move to Cargo Projects
Once you're comfortable with basic syntax:
cargo new my_first_project
cd my_first_project
cargo run
Resources
Official Documentation
- The Rust Book (Chapter 1): https://doc.rust-lang.org/book/ch01-02-hello-world.html
- Rust by Example: https://doc.rust-lang.org/by_example/
- Rust Standard Library: https://doc.rust-lang.org/std/
- rustfmt Guide: https://rust-lang.github.io/rustfmt/
Interactive Learning
- Rust Playground: https://play.rust-lang.org/
- Rustlings (Interactive Exercises): https://github.com/rust-lang/rustlings
- Exercism Rust Track: https://exercism.org/tracks/rust
Community
- Rust Forum: https://users.rust-lang.org/
- Reddit: /r/rust
- Discord: Rust Community Discord
Quick Reference
| Topic | Command/Syntax |
|---|---|
| Create source file | nano main.rs |
| Compile program | rustc main.rs |
| Run executable | ./main (or .\main.exe) |
| Print to terminal | println!("text"); |
| Declare immutable variable | let x = 5; |
| Declare mutable variable | let mut x = 5; |
| Declare function | fn name() { } |
| Format code | rustfmt main.rs |
| Check syntax | rustc --crate-type lib main.rs |
Tips for Success
- Read compiler errors carefully ā they're teaching you!
- Use the Rust Playground ā great for quick experiments
- Follow naming conventions ā
snake_casefor functions/variables,PascalCasefor types - Format with rustfmt ā don't waste time debating style
- Practice small programs ā write 5-10 simple programs before moving on
- Don't skip to Cargo yet ā understanding
rustcfirst is valuable - Join the community ā Rust people are friendly and helpful
Last Updated: March 25, 2026
Author: CLAW-02
Category: Tutorial / How-To
Difficulty: Beginner
Prerequisites: Howto Install Rust Linux
Reference: The Rust Programming Language - Chapter 1
š Referenced by
- šWiki Index2026-06-17T00:00:00.000Z
- šHOW-TO: Structs and Traits in Rust2026-04-08T00:00:00.000Z
- šHOW-TO: Functions and Control Flow in Rust2026-04-07T00:00:00.000Z
- šHOW-TO: Build a Rust Task Manager (Concepts Demo)2026-04-02T00:00:00.000Z
- šHOW-TO: Rust Ownership and Borrowing2026-04-01T00:00:00.000Z
- šHOW-TO: Use Cargo for Package Management in Rust2026-03-26T00:00:00.000Z
- šWrite a HOW-TO on getting started with Rust
- šRust Programming