SYSTEMS PROGRAMMING

Build Fast.
Stay Safe.

Rust is a tool to tell computers what to do. It fixes the biggest problem in coding: it stops computers from crashing and keeps hackers out, all while running incredibly fast.

Quick Facts

  • No random crashes.
  • Super fast (like C++).
  • Very strict rules.
  • Used to build game engines.

The Big Problem in Coding

Computer Memory is a Box

When a program runs, it needs space to keep information. Think of computer memory as a giant wall of empty boxes.

In older fast languages, programmers had to manually say, "Give me a box" and then remember to say "I am done with the box."

Human Error

Humans forget things. If a programmer forgets to give the box back, the computer runs out of space and crashes. If they try to use a box they already gave back, the computer gets confused and breaks. Hackers love these mistakes.

How Do We Fix This?

1. The Manual Way

Examples: C, C++

The programmer does all the work. It is very fast, but very dangerous because humans make mistakes.

2. The Garbage Collector

Examples: Java, Python

A little robot follows the program around and cleans up unused boxes. It is very safe, but the robot slows the program down.

3. The Rust Way

Example: Rust

Rust uses strict "Ownership Rules" before the program even runs. It is very safe AND very fast. No cleaning robots needed.

The Ownership Rules

This is how Rust keeps memory safe without slowing down. Think of it like rules for lending a toy.

1

One Owner

Every piece of data has exactly one owner at a time. No sharing ownership.

2

Move or Die

If the owner goes away, the data is instantly thrown in the trash. No leaks, ever.

3

Hand It Over

You can give the data to someone else, but then you are not allowed to use it anymore.

Borrowing Data

What if you want to let a friend read your book, but you want it back? Rust calls this Borrowing.

  • Rule 1: Any number of people can read the data at the same time.
  • Rule 2: If someone wants to change the data, everyone else must close their eyes. Only one person can change it at a time.
// Code Example: Borrowing
let my_book = String::from("getbetterat.work");

// We lend the book out. We use the '&' symbol.
let friend = &my_book;

// We can still read it because we only lent it for reading.
println!("I still have {}", my_book);

The Strict Teacher

In most languages, you write code, run it, and it crashes later when a user is using it.

Rust has a tool called the Compiler. It checks your code before it is allowed to run. If you break an ownership rule, it stops you and says exactly how to fix it.

1

Write Code

2

Compiler Check

3

Run Fast

error[E0382]: borrow of moved value: `my_toy`
The compiler stops bad code from ever running!

Locked by Default

Normal Variables

When you put a value in a box, Rust locks the box. You cannot change what is inside. This stops accidental overwriting.

let score = 10;
// score = 20; <-- This causes an ERROR!

Mutable Variables

If you know the value needs to change, you must explicitly tell Rust using the magic word mut.

let mut score = 10;
score = 20; // This is totally fine!

What's in the Box? (Data Types)

Rust needs to know exactly what kind of item you are putting in the box so it knows how much space to make.

42
Integers

Whole numbers.

3.14
Floats

Numbers with dots.

true
Booleans

True or False only.

"Hi"
Strings

Letters and words.

Functions (Mini-Workers)

Functions are like mini-workers in your factory. You give them some raw materials (Inputs), they do a specific job, and they give you a finished product (Output).

Input: 5 and 10
fn add_numbers(a: i32, b: i32) -> i32 {
    // Worker logic goes here
    return a + b;
}
15

Making Choices

Computers are smart because they can make choices. If a condition is true, it goes down one path. If it is false, it goes down another path.

This is called If / Else. Think of it like train tracks switching directions based on a signal.

if weather == "raining" {
// Take an umbrella
} else if weather == "sunny" {
// Wear sunglasses
} else {
// Just stay inside
}

The Merry-Go-Round (Loops)

Sometimes you want the computer to do the same task 100 times. Instead of writing the code 100 times, you put it in a loop.

loop

Runs forever until you explicitly say "STOP" (break).

loop {
  print!("Hello!");
}

while

Runs over and over AS LONG AS something is true.

while power > 0 {
  power -= 1;
}

for

Goes through a list of items, one by one, until the end.

for item in box {
  look_at(item);
}

Build Your Own Box (Structs)

User ID Card
Name: "Alex"
Level: 99
Active: true

Sometimes numbers and text aren't enough. What if you want to store a whole "Player" in a game?

A Struct lets you build a custom master-box that holds lots of smaller boxes inside it. It groups related data together.

struct Player {
    name: String,
    level: u32,
    is_active: bool,
}

The Menu (Enums)

An Enum (short for Enumeration) is like a restaurant menu. It gives the computer a strict list of choices. You must pick exactly one item from the menu.

For example, a traffic light can only be Red, Yellow, or Green. It can never be Purple. Enums force the computer to only accept valid choices.

TrafficLight Enum

  • RED
  • YELLOW
  • GREEN

The Switchboard (Match)

Once you have an Enum (a choice), you need a way to tell the computer what to do for EACH choice. Rust uses match.

match current_light {
    TrafficLight::Red => {
        stop_car();
    },
    TrafficLight::Green => {
        go_car();
    },
    TrafficLight::Yellow => {
        slow_down();
    }
}

No Missing Pieces

The strict teacher (Compiler) loves `match`. If you add a "Blue" light to your traffic light Enum, but forget to update the `match` block, the program will refuse to run. It forces you to handle every single possible choice.

No Secret Explosions

Other Languages

If a program tries to open a file that doesn't exist, it usually panics and crashes instantly. Or worse, it returns a silent "null" invisible error that breaks things later.

The Result Box

In Rust, dangerous actions (like opening a file) don't give you the file right away. They give you a sealed box called a Result.

The sealed box either contains Ok(file) OR Err(error_note). Rust forces you to write code to check which one it is before you can proceed.

The Mega Store (Crates)

You do not have to build everything from scratch. If you need your program to read a JSON file, create a web server, or make game graphics, someone else has already written that code.

In Rust, these bundles of code are called Crates. There is a massive, free website called Crates.io where millions of developers share their tools. Remember our friend Cargo? Just tell Cargo the name of the crate, and it downloads it automatically.

serde
tokio
rand

Why Pick Rust?

Old Way

Speed OR Safety

Historically, you had to make a choice. Write in C/C++ to go fast but risk massive bugs. Or write in Python/Java to be safe, but run much slower.

The Rust Way

Speed AND Safety

Because Rust does all its safety checks before the program runs, the final program does not carry any heavy cleaning tools. It runs as fast as C++, but is totally safe.

Where is Rust Used?

Game Engines

Games need to process graphics instantly. Rust is fast enough for games but stops them from crashing.

Security

Hackers break into systems by exploiting memory bugs. Rust makes those bugs impossible.

Servers

Companies like Amazon and Cloudflare use Rust to run massive servers safely and cheaply.

Operating Systems

Even the core brains of computers (like Linux and Windows) are starting to use Rust for safety.

Meet Cargo

Learning a new language is usually hard because you have to download dozens of tools to build your code. Rust makes it easy.

Rust comes with Cargo. Cargo is your personal helper. It downloads other people's code for you, tests your code, and builds your final program with just one command.

$ cargo new my_project
# Cargo creates a folder for you.
$ cd my_project
$ cargo run
# Cargo builds and runs the code safely!
Hello, getbetterat.work!
main.rs
// A simple Rust program demonstrating safety
fn main() {
    // We create a new piece of data. 
    // The variable 'message' is the OWNER.
    let message = String::from("Safety First!");

    // We pass the message to a function to print it.
    // We use '&' to LEND it. We do not give away ownership.
    print_message(&message);

    // Because we only lent it, we can still use it here!
    println!("We still own the data: {}", message);
}

fn print_message(borrowed_text: &String) {
    println!("Printing: {}", borrowed_text);
}