Rust at a glance · 9 min · free lesson

Node.js runtime vs compiled Rust

Rust is compiled ahead of time to a native executable: rustc checks types, ownership and borrowing before anything runs, and there is no garbage collector or built-in event loop.

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.

JavaScript (Node.js) and Rust, side by side

JavaScript (Node.js) Node.js runs the file through V8, so the typo in the EUR branch surfaces as a TypeError only when that branch runs.
// node app.js: V8 parses, interprets and JIT-compiles this while it runs.
function total(prices, currency) {
  const sum = prices.reduce((acc, p) => acc + p, 0);
  if (currency === "EUR") {
    return sum.toFixd(2) + " EUR"; // typo: fails only when this branch runs
  }
  return String(sum);
}

console.log(total([3, 4], "USD"));
console.log(process.release.name, typeof process.versions.v8);
try {
  total([3, 4], "EUR");
} catch (err) {
  console.log(err.constructor.name, err.message);
}
Rust rustc checked both branches before this ran. The last line names the compile target: macos aarch64 on an Apple silicon Mac, wasi wasm32 for the app's WebAssembly target.
// rustc main.rs (or cargo build) checks everything first, then writes an executable.
fn total(prices: &[u32], currency: &str) -> String {
    let sum: u32 = prices.iter().sum();
    if currency == "EUR" {
        format!("{sum}.00 EUR")
    } else {
        sum.to_string()
    }
}

fn main() {
    println!("{}", total(&[3, 4], "USD"));
    println!("{}", total(&[3, 4], "EUR"));
    println!("{} {}", std::env::consts::OS, std::env::consts::ARCH);
}
What changes
JavaScript (Node.js)Rust
node app.js needs Node.js on every machine that runs it.cargo build writes a native executable, or a WASM module, that runs without Rust or Node.js installed.
A typo in a branch is a TypeError only when that branch runs.rustc checks every branch first; the same typo is E0599, and no executable is built.
V8 interprets and JIT-compiles while the program runs.Compilation finishes before the program starts, so there is no JIT warm-up.
The garbage collector and the event loop come with the runtime.There is no garbage collector and no built-in event loop; async code uses a library such as Tokio.

Understand it

node app.js starts a runtime. V8 parses your JavaScript, interprets it and compiles hot functions to machine code while the program runs, and the Node.js process supplies the event loop, the garbage collector and APIs such as fs. Every machine that runs the program needs Node.js too. Rust works ahead of time: rustc, usually driven by cargo build, reads the whole crate, checks it and writes a native executable for one target, such as macOS on Apple silicon or Linux on x86-64, or a WebAssembly module as in the Crabrix app. The result runs without Rust or Node.js installed.

Checking happens before anything runs. rustc verifies types, ownership and borrowing in every function, including branches your tests never reach, and produces no executable while an error remains. The typo in the JavaScript EUR branch is a TypeError on the day that branch runs; the same typo in Rust is E0599 at build time. There is no separate tsc step to skip and no way to run the program anyway.

Two things you take for granted are not built in. There is no garbage collector: each value has an owner and is freed at a known point, which the next lesson shows. There is no event loop either: async code needs a runtime library such as Tokio, chosen by the program. In exchange, a Rust executable has no JIT warm-up, and its memory use follows from the code rather than from collector heuristics.

Static typing in Rust is mostly inferred. You annotate function signatures and struct fields; inside a function most let bindings need no annotation, and let sum: u32 = prices.iter().sum(); needs one only because sum can produce several types. The types shape the compiled program: a u32 occupies four bytes, and adding two of them compiles to an integer addition.

The JavaScript (Node.js) habit, written in Rust

This is how the JavaScript (Node.js) version usually looks on a first attempt in Rust. Predict what rustc says before you read the explanation.

fn total(prices: &[u32], currency: &str) -> String {
    let sum: u32 = prices.iter().sum();
    if currency == "EUR" {
        return sum.to_strin() + " EUR";
    }
    sum.to_string()
}

fn main() {
    println!("{}", total(&[3, 4], "USD"));
}

Practise this code in Playground ↗

Try it

Compile the practice code and read the error before running anything. Fix the typo, build again, and note that the program now runs although nothing ever calls the EUR branch.

What success looks like: You can explain why an error appears in a branch that never runs, and what file rustc or cargo build produces.

Check your understanding

The EUR branch of the practice code has a typo, and your test only calls total(&[3, 4], "USD"). What happens?

Reveal the answer
  1. rustc refuses to build the program and reports E0599 for that branch, although it would never run
  2. The program runs and fails only if the EUR branch executes, like the TypeError in Node.js
  3. rustc prints a warning, and the executable skips the broken branch

rustc checks every function and branch before it writes an executable. The typo is a compile error, E0599, whether or not a test reaches it.

Go deeper

Common misconception: “Rust is like TypeScript: the types are checked, but the program still runs on a runtime such as Node.js.”

rustc compiles the checked program ahead of time to machine code or WebAssembly. The executable contains no interpreter, JIT or garbage collector, and nothing like node is needed to run it.

  1. Build before running: rustc main.rs or cargo build reads the whole program. Nothing runs yet; total and main are checked completely.
  2. Check every branch: Both arms of the if are type-checked, although the USD call never takes the EUR arm. A typo in either would stop the build.
  3. Compare with evidence: Run it: 7, 7.00 EUR, then the target, macos aarch64 on an Apple silicon Mac. Compile the practice code and read E0599 instead.

Transfer challenge: Build the example with cargo build --release and find the executable in target/release/. Copy it to another machine with the same OS and CPU architecture and no Rust installed, run it, and explain why a Linux server needs a binary built for Linux.

Connections

  • Garbage collection vs deterministic drop: Deterministic drop at the end of scope
Side-by-side comparison: the JavaScript (Node.js) version on the left, the Rust version on the right, then 4 rows naming what changes.
Node.js runtime vs compiled Rust: the same task in JavaScript (Node.js) 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: Node.js runtime vs compiled Rust (courseId "rust-for-nodejs-developers", lessonId "node-what-rust-is")
Link: https://crabrix.com/learn/rust-for-nodejs-developers/node-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-nodejs-developers/node-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.