Rust at a glance · 10 min · free lesson

What Rust Is, from C

Rust compiles to native code like C, with no garbage collector and no virtual machine; what changes is that rustc proves memory safety for safe code before the program runs.

Core idea

Rust is a compiled, statically typed language without a garbage collector: rustc checks types, ownership and borrowing before the program runs, then produces a native executable.

C and Rust, side by side

C Stack value, heap copy and an aliasing pointer; after free, nothing stops a later use of alias.
#include <stdio.h>
#include <stdlib.h>

struct point { int x, y; };

int main(void) {
    struct point on_stack = {1, 2};                    /* a stack slot */
    struct point *on_heap = malloc(sizeof *on_heap);   /* heap, freed by hand */
    if (!on_heap) return 1;
    *on_heap = on_stack;
    on_heap->x = 10;
    struct point *alias = on_heap;                     /* any pointer may alias */
    printf("%d %d %d %zu\n", on_stack.x, alias->x, alias->y, sizeof(struct point));
    free(on_heap);
    /* alias still holds the address; nothing stops a later *alias. */
    return 0;
}
Rust The same stack value, heap copy and pointer as in C; the Box frees its memory when its owner goes out of scope, and the borrow alias is checked.
#[derive(Clone, Copy)]
struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let on_stack = Point { x: 1, y: 2 };  // a stack slot, as in C
    let mut on_heap = Box::new(on_stack); // heap memory with one owner
    on_heap.x = 10;
    let alias = &on_heap;                 // a borrow the compiler tracks
    println!("{} {} {} {}", on_stack.x, alias.x, alias.y, size_of::<Point>());
}                                         // the Box is freed here, exactly once
What changes
CRust
clang compiles C ahead of time through LLVM to a native executable.rustc also compiles ahead of time through LLVM to a native executable.
No garbage collector; memory is freed by explicit free calls.No garbage collector; the compiler inserts the free where the owner goes out of scope.
The compiler accepts any pointer use that type-checks.rustc also checks ownership and borrowing, and rejects what it cannot prove safe.

Understand it

From a C programmer's seat, most of the toolchain looks familiar. rustc is an ahead-of-time compiler: it parses and type-checks the program, lowers it to LLVM IR, and LLVM, the same optimiser and code generator family that clang uses, emits machine code that the platform linker turns into an ordinary executable. There is no interpreter, no virtual machine and no garbage collector. The standard library is linked into the binary by default, and its start-up work before main is small, comparable to what the C runtime and libc already do; #![no_std] code for kernels and microcontrollers drops even that and keeps only core.

The machine model is the same as C's. Locals live in stack frames, Box, Vec and String allocate on the heap through the system allocator, references and raw pointers are addresses, integers have fixed widths, and structs have a size you can ask for: size_of::<Point>() is 8, like sizeof(struct point). You reason about performance the way you do in C, and Rust can call C and be called from C.

The difference is what the compiler checks. Besides static types, rustc tracks ownership (which binding frees each value) and borrowing (which references may read or write it, and for how long). For safe code it proves, before the program runs, that no value is used after it is freed, freed twice, or written while another reference reads it, and that no data race is possible. Those checks happen at compile time and cost nothing at run time; the remaining run-time checks, such as array bounds, are explicit and cheap.

The price is that rustc rejects programs it cannot prove safe, even some that would happen to work. The C program frees on_heap while alias still holds its address, and clang accepts any later use of alias; the same shape in Rust is error E0505, because on_heap cannot be moved into drop while alias still borrows it. When safe Rust cannot express a design, unsafe lets you take over specific proofs yourself, as C always required.

The C habit, written in Rust

This is how the C version usually looks on a first attempt in Rust. Predict what rustc says before you read the explanation.

fn main() {
    let on_heap = Box::new(5);
    let alias = &on_heap;
    drop(on_heap); // the C habit: free while a pointer is still in use
    println!("{alias}");
}

Practise this code in Playground ↗

Try it

Compile the practice program, read why rustc refuses it, and say where the equivalent C program would fail instead.

What success looks like: You can explain that the C version compiles and is undefined at run time, while rustc rejects the Rust version with E0505 before any code runs.

Check your understanding

Where does Rust's memory safety come from, compared with C?

Reveal the answer
  1. A garbage collector that frees unreachable memory at run time
  2. rustc checks types, ownership and borrowing at compile time and then emits a native executable through LLVM
  3. A virtual machine that checks every pointer access while the program runs
  4. Every pointer is reference-counted automatically

The checks are static: rustc proves them while compiling, then produces ordinary machine code. There is no collector, VM or hidden reference counting.

Go deeper

Common misconception: “Rust is safe because it runs in a managed runtime, like Java or C#.”

Rust has no managed runtime. Safety comes from compile-time checks of ownership and borrowing; the output is ordinary machine code with C-like performance, and memory is freed at points the compiler inserts, not by a collector.

  1. Map the toolchain: clang → LLVM → linker becomes rustc → LLVM → linker; both produce a native executable you can inspect with file or otool.
  2. Map the memory: Stack locals stay stack locals; malloced objects become Box, Vec or String, each with one owner that frees it.
  3. Find the new check: Look for the point where the C program relies on a comment or discipline; that is where rustc will ask for a proof.

Transfer challenge: Build the example with rustc -O and the C program with clang -O2, then run file on both binaries and compare size_of::<Point>() with sizeof(struct point).

Connections

  • What Safe Rust Rules Out: Undefined behaviour excluded, bugs still possible
Side-by-side comparison: the C version on the left, the Rust version on the right, then 3 rows naming what changes.
What Rust Is, from C: the same task in C and in Rust, with the differences that matter.
Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: What Rust Is, from C (courseId "rust-for-c-developers", lessonId "c-what-rust-is")
Link: https://crabrix.com/learn/rust-for-c-developers/c-what-rust-is/
Goal: Rust is a compiled, statically typed language without a garbage collector: rustc checks types, ownership and borrowing before the program runs, then produces a native executable.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=rust-for-c-developers/c-what-rust-is) and I will write the code there.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.