JavaScript (Node.js) and Rust, side by side
makeBuffers returns, but the collector decides when to free them, and here it never runs before the process exits.const registry = new FinalizationRegistry((name) => console.log(`collected ${name}`));
function makeBuffers() {
const first = { name: "first", data: new Array(1000).fill(0) };
const second = { name: "second", data: new Array(1000).fill(0) };
registry.register(first, "first");
registry.register(second, "second");
console.log("leaving makeBuffers");
} // both objects are unreachable now, but nothing is freed yet
makeBuffers();
setTimeout(() => console.log("later: the collector decides when, if ever"), 10);drop(early) releases a value before its scope ends.struct Noisy(&'static str);
impl Drop for Noisy {
fn drop(&mut self) {
println!("drop {}", self.0);
}
}
struct Pair {
left: Noisy,
right: Noisy,
}
fn make_buffers() {
let _first = Noisy("first");
let _second = Noisy("second");
let _pair = Pair { left: Noisy("pair.left"), right: Noisy("pair.right") };
println!("leaving make_buffers");
}
fn main() {
make_buffers();
let early = Noisy("early");
drop(early);
println!("end of main");
}| JavaScript (Node.js) | Rust |
|---|---|
| V8 frees an unreachable object whenever its collector runs. | A value is dropped at the end of its owner's scope, at a line you can point to. |
A FinalizationRegistry callback may run late or never. | Drop::drop runs exactly once, when the owner goes away or drop(value) is called. |
Files and sockets need an explicit close() or try/finally. | File, TcpStream and lock guards release their resource in Drop. |
| Collection pauses happen whenever the runtime decides. | There is no collector and no collection pause; freeing happens where the scope ends. |
Understand it
In Node.js, V8's garbage collector frees an object at some point after it becomes unreachable. The language leaves that point open: a FinalizationRegistry callback may run late, or not at all before the process exits, which is what the example shows. That is fine for memory, but it is why files, sockets and locks need an explicit close(), try/finally or await handle.close(); a forgotten handle stays open until the collector or the process exit gets to it.
Rust has no collector. Every value has one owner, and when the owner goes out of scope the value is dropped right there: its Drop implementation runs, then its fields are dropped, then its memory is released. The order is defined. At the end of a scope the remaining locals drop in reverse declaration order, so the last one created is cleaned up first, and inside a struct the fields drop in declaration order. A value that was moved elsewhere is dropped by its new owner instead, exactly once.
Because cleanup is tied to scope, one mechanism manages every resource. A File closes its descriptor in Drop, a MutexGuard unlocks its mutex, a TcpStream closes its socket and a Vec frees its buffer. There are no collection pauses and no finalizer threads; the cost of freeing appears at the closing brace where it happens.
To release something earlier, end its scope or call drop(value), the function std::mem::drop, which takes ownership and lets the value go. You never call the drop method yourself; rustc rejects that with E0040, because the destructor must run exactly once. Leaks are still possible, for example through Rc reference cycles or std::mem::forget, but they are explicit choices rather than an accident of the collector's timing.
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.
struct Connection(&'static str);
impl Drop for Connection {
fn drop(&mut self) {
println!("closing {}", self.0);
}
}
fn main() {
let conn = Connection("db");
conn.drop();
println!("done");
}Try it
Predict the order of all seven lines of the example before running it. Then move let _second into an inner block of its own and predict again.
What success looks like: Your predicted order matches the output, including the two pair fields, and you can say where each value's memory is released.
Check your understanding
In the example, which message prints right after leaving make_buffers?
Reveal the answer
drop first, because_firstwas declared first- Nothing yet: the memory is freed later, whenever the allocator decides
drop pair.left: the last local drops first, and its fields drop in declaration order
At the closing brace, _pair is the last declared local, so it drops first, left before right; then _second and _first follow.
Go deeper
Common misconception: “Rust frees memory at some later point, like a garbage collector, only faster.”
There is no later point. A value is dropped exactly when its owner's scope ends, or when it is passed to drop, and the order follows the declarations.
- List the owners:
make_buffersowns_first,_secondand_pair, and_pairowns twoNoisyfields.mainownsearly. - Close the scope: At the closing brace of
make_buffers,_pairdrops first,pair.leftand thenpair.right; then_second, then_first. - Compare with evidence: Run it:
leaving make_buffers, the four drops in that order,drop early, thenend of main. Nothing is left for later.
Transfer challenge: Return _second from make_buffers and bind it in main. Predict where drop second moves in the output, and say which owner drops it now.
Connections
- Node.js runtime vs compiled Rust: Ahead-of-time compilation to a native executable
- TypeScript types vs Rust types: Types the compiler enforces, and its diagnostics

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: Garbage collection vs deterministic drop (courseId "rust-for-nodejs-developers", lessonId "node-no-garbage-collector") Link: https://crabrix.com/learn/rust-for-nodejs-developers/node-no-garbage-collector/ Goal: At scope end, remaining locals drop in reverse declaration order; struct fields drop in declaration order. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=rust-for-nodejs-developers/node-no-garbage-collector) 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.