almessadi.
Back to Index

Rust's Borrow Checker for JavaScript Developers_

The easiest way to understand Rust ownership is to stop comparing it to syntax and start comparing it to object lifetime.

PublishedMay 8, 2024
Reading Time7 min read

What makes Rust feel hostile to JavaScript developers is not the syntax. It is the lifetime model.

In JavaScript, you usually do not think directly about who owns memory. The runtime and garbage collector manage that for you.

Rust forces the question into the type system.

Ownership in One Sentence

Each value has an owner, and when that owner goes out of scope, the value is dropped.

That is the core model.

The compiler then enforces borrowing rules so you cannot accidentally use data after it was moved or mutate it through conflicting references.

The First Error Everyone Sees

fn print_name(name: String) {
    println!("{}", name);
}

fn main() {
    let my_name = String::from("Alaeddine");
    print_name(my_name);
    println!("{}", my_name);
}

This fails because my_name is moved into print_name.

For a JavaScript developer, that feels arbitrary. For Rust, it is how ownership transfer is made explicit.

Borrowing Instead of Moving

If the function only needs to read the value, borrow it:

fn print_name(name: &str) {
    println!("{}", name);
}

fn main() {
    let my_name = String::from("Alaeddine");
    print_name(&my_name);
    println!("{}", my_name);
}

Now ownership stays with my_name, and the function receives a reference with a narrower promise.

Why This Exists

The borrow checker is not trying to make you suffer. It is preventing:

  • use-after-free bugs
  • data races
  • invalid aliasing patterns

in code that would otherwise compile in lower-level languages and fail at runtime.

The ergonomic cost is upfront. The benefit is that whole classes of memory bugs become compilation errors.

The Best Way to Learn It

Do not start by memorizing error messages.

Start by asking:

  • who owns this value?
  • am I moving it or borrowing it?
  • does this function need ownership, mutation, or only a read reference?

Once those questions become normal, the compiler errors start reading like guidance instead of punishment.

Further Reading