Skip to main content

Your First Rust Program

Let’s get you up and running with Rust in just a few minutes.
1

Verify your installation

First, make sure Rust is properly installed:
rustc --version
cargo --version
You should see version information for both the Rust compiler (rustc) and the package manager (cargo).
If you don’t have Rust installed yet, head over to the Installation page.
2

Write Hello World

Create a new file called main.rs:
fn main() {
    println!("Hello, world!");
}
This is a simple Rust program that prints “Hello, world!” to the console.
3

Compile and run

Compile your program:
rustc main.rs
This creates an executable called main (or main.exe on Windows). Run it:
./main
You should see:
Hello, world!

Creating a Project with Cargo

While rustc is great for simple programs, most Rust developers use Cargo - Rust’s build tool and package manager.
1

Create a new project

cargo new hello_rust
cd hello_rust
This creates a new directory with the following structure:
hello_rust/
├── Cargo.toml
└── src/
    └── main.rs
2

Explore the project structure

Cargo.toml - The manifest file containing metadata and dependencies:
[package]
name = "hello_rust"
version = "0.1.0"
edition = "2021"

[dependencies]
src/main.rs - Your source code (already contains a “Hello, world!” program)
3

Build and run with Cargo

Build your project:
cargo build
Or build and run in one command:
cargo run
Use cargo build --release for optimized builds. Debug builds are faster to compile but slower to run.

Essential Cargo Commands

Here are the most important Cargo commands you’ll use:

cargo new

Create a new Cargo project
cargo new my_project

cargo build

Compile your project
cargo build
cargo build --release

cargo run

Build and run your project
cargo run

cargo test

Run your project’s tests
cargo test

cargo check

Check if your code compiles (faster than build)
cargo check

cargo doc

Build documentation for your project
cargo doc --open

Adding Dependencies

Cargo makes it easy to use external libraries (called “crates” in Rust).
1

Add a dependency

Edit Cargo.toml and add a dependency:
[dependencies]
serde = "1.0"
serde_json = "1.0"
2

Use the dependency

Import and use it in your code:
use serde_json::json;

fn main() {
    let data = json!({
        "name": "Rust",
        "type": "programming language"
    });
    println!("{}", data);
}
3

Build and run

cargo run
Cargo automatically downloads and compiles dependencies on the first build.
Search for crates on crates.io, the official Rust package registry.

Essential Tools

Rust comes with powerful tools to improve your development experience:

rustfmt - Code Formatter

Automatically format your code to follow Rust style guidelines:
rustfmt src/main.rs
Or format your entire project:
cargo fmt

Clippy - Linter

Catch common mistakes and improve your code:
cargo clippy
Clippy provides hundreds of lints to help you write idiomatic and efficient Rust code.

rust-analyzer - IDE Support

For the best development experience, install the rust-analyzer extension for your editor:
  • VS Code: Search for “rust-analyzer” in extensions
  • IntelliJ/CLion: Rust plugin with rust-analyzer support
  • Vim/Neovim: Use CoC or native LSP support
  • Emacs: Use lsp-mode or eglot
rust-analyzer provides real-time error checking, code completion, inline documentation, and more.

Understanding Rust’s Type System

Here’s a quick example showing Rust’s powerful type system and ownership model:
fn main() {
    // Variables are immutable by default
    let x = 5;
    // x = 6; // This would cause a compile error!
    
    // Use 'mut' for mutable variables
    let mut y = 5;
    y = 6; // This is fine
    
    // Type inference works, but you can be explicit
    let z: i32 = 42;
    
    // String ownership example
    let s1 = String::from("hello");
    let s2 = s1; // s1 is moved to s2
    // println!("{}", s1); // This would error - s1 is no longer valid
    println!("{}", s2); // This works
}
Rust’s ownership system is one of its most unique features. It ensures memory safety without garbage collection, but it requires learning new concepts. Don’t worry if it seems confusing at first - it becomes natural with practice!

Next Steps

Now that you have Rust set up and running, here are some great resources to continue learning:

The Rust Book

The official comprehensive guide to Rust

Rust by Example

Learn Rust through annotated example programs

Rustlings

Small exercises to get you used to reading and writing Rust

Standard Library Docs

Complete documentation for the Rust standard library

Getting Help

If you run into issues or have questions:
The Rust community is known for being friendly and welcoming to newcomers. Don’t hesitate to ask questions!

Contributing to Rust

Interested in contributing to the Rust compiler itself?
There are many ways to contribute to Rust beyond just writing code - documentation, testing, triaging issues, and helping others are all valuable contributions!

Build docs developers (and LLMs) love