Skip to main content
Be Bitwise
RustRust Basics & Ownership

Hello, Rust!

Rust is a systems language that gives you memory safety without a garbage collector breathing down your neck. The trick? An ownership system — compile-time rules that erase use-after-free, double-free, and data races before your binary ever boots.

Your First Rust Program

rust
fn main() {
println!("Hello, Rust!");
}
  • fn main() is the entry point, same as in C.
  • println! is a macro — that's what the ! is telling you — and it writes to stdout.
  • The last expression in a block, sans semicolon, becomes the block's value.

Run it, then make it yours. The second half shows that last bullet in action: the inner block's final line has no semicolon, so its value lands in answer.

Try itChange the greeting and add a second println!, then put a semicolon after base + 2 and read what the compiler says.
Rust (1.40.0)

Compilation

bash
rustc hello.rs
./hello

Or with Cargo, Rust's build tool and package manager:

bash
cargo new hello-project
cd hello-project
cargo run

Either way, the same pipeline runs underneath. rustc does the Rust-specific work up front (macros, type checking, the borrow checker), then hands an intermediate form to LLVM, the same backend Clang uses for C. LLVM optimises it and emits machine code into an object file, and a system linker stitches that together with the standard library into an executable:

All the safety checks live in that second box and leave nothing behind in the binary. Module 7, What Your Rust Code Does, opens the output up with a disassembler to prove it.

Key Concept

Rust's compiler is notoriously strict — it intercepts at compile time what C and C++ leave for the segfault. The error messages have grown into something rare in this industry: actually helpful. They explain what broke, why it broke, and frequently hand you a patch. Read them as tutoring, not nagging.

Variables

Variables are immutable by default:

rust
let x = 5;
// x = 6; // ERROR: cannot assign twice to immutable variable
let mut y = 5;
y = 6; // OK: y is declared as mutable

Types

Rust has strong, static typing with inference:

rust
let x: i32 = 42; // explicit type
let y = 42; // inferred as i32
let pi: f64 = 3.14159;
let flag: bool = true;
let ch: char = 'A';
let name: &str = "Alice";
let owned: String = String::from("Alice");

Why Rust for Low-Level Learning?

Rust hands you the same control C does — manual memory layout, zero-cost abstractions, no GC — and the compiler bins the bugs that turn C into a footgun:

  • No null pointers; Rust models absence with Option<T>.
  • No dangling pointers; the borrow checker refuses to let you have them.
  • No data races; the type system enforces it across threads.
  • No buffer overflows; bounds checks are the default and you opt out, not in.

By the end of this track you'll have built a command-line network tool, picking up Rust's quirks along the way.