You know JavaScript. This is Rust, taught from there, and compiled back to JavaScript with rust-js, so what you write still runs where your JavaScript does: in the browser, in Node, in bun.
Each chapter takes one idea, writes it in JavaScript and in Rust, compiles the Rust to JavaScript, and runs both on the same inputs, so you can see where they agree and where they don't. Native Rust runs the same inputs too: the JavaScript rust-js writes must say what native Rust says.
This guide assumes JavaScript, not TypeScript. Types are new here, and the early chapters introduce them from scratch: what a type is, why a compiler checks one before the program runs, and what that buys you. If you already write TypeScript, coming-from-typescript starts further along.
- The same function, twice: three JavaScript functions and their Rust, how to read the Rust, and the JavaScript rust-js writes, which reads like yours.
- Types are checked before it runs:
what a type is, the typo and the
"2" + 1the compiler stops before anything runs,parseas a step you write, and inference. let,constandmut: nothing changes unless it saysmut, contents included, and a function that changes its argument says&mutwhere it's written and called.- Numbers: one JavaScript
numberbecomes a kind for each job, whole numbers that divide whole,asfor every change of kind, overflow you decide on, andu64as abigint. - Strings are UTF-8: why
lengthcounts an emoji twice andslicecuts it in half,chars()for characters, andStringand&str. - Everything is an expression:
if, blocks andloopthat give values, andmatch, aswitchthat must cover every case and never falls through. - Objects are structs: a
struct names its fields once, methods take
selfas a parameter, and a typo'd field or a lostthiscan't happen. - Equality:
==that compares what's inside, never values of two types,includesand "unique" that work on objects, and an order a type derives. - String flags are enums:
a state as an enum whose variants carry their own data, a typo that
doesn't compile, and a
matchthat must handle every state. nullandundefinedareOption: a type for "may be missing",unwrap_orthat keeps a 0 where||doesn't, and?for giving up early.try/catchisResult: failure in the return type, an error enum naming every way,?to pass it on, and panics kept for bugs.- Arrays and iterators:
a
Vecowns and a slice borrows, so a function that reads can't sort your array; lazy iterators, andOptions where JavaScript gives-Infinityandundefined. Map,Set, and objects as maps: the word "constructor" a plain object can't count,HashMapandHashSetwith one key type,entry, and order you ask for.- Functions and closures:
closures that copy a loop's
iwherevarshares one,move, andFnandFnMutsaying what a closure does to what it holds. - Duck typing is traits:
a trait lists what a type can do and an
implmust have it all, so a misspelledsendis caught before a broadcast half-sends;implanddyn. - Generics: one function for many
types with a bound that says what a
Tcan do, where JavaScript'slargestanswers for objects anyway; generic structs. - Pattern matching: a URL
router by the shape of its parts, slice patterns,
..,@, guards andlet else, where JavaScript's makes a page for user NaN. - Moves: one owner, so a match's
players can't be changed behind its back;
cloneandCopy, and what JavaScript callers should know today. - Borrowing:
&to read and&mutto change, many readers or one writer, so asplicein a loop and a reference into a growing list don't compile. RcandRefCell: sharing on purpose, a cart the header and the checkout both own, borrows checked as the program runs.- npm is cargo:
moddeclares a module,pub(crate)is a privacy JavaScript lacks,pub useis the front door, and a crates.io dependency compiled to JavaScript too. async: the same words, a forgottenawaitthat doesn't compile where JavaScript says "Hello, undefined!", and futures that wait where promises start.- Testing:
#[test]built in, unit tests beside the code that reach its private helpers,should_panic, and the same Rust tests run as JavaScript byrust-js test. - Calling JavaScript, and where JavaScript is easier: a binding to your own JavaScript, a JavaScript callback as a closure, and an honest list of what JavaScript makes easier.
- Calling an npm package: building its arguments:
every shape a package asks for, an options object, a callback, a
union,
null, rest arguments, a record, a class, each built in Rust beside the JavaScript rust-js writes, and what nothing checks.
You need bun and Rust. The
toolchain is pinned in rust-toolchain.toml, and rustup fetches it.
git clone --recurse-submodules https://github.com/rust-js-lang/coming-from-javascript
cd coming-from-javascript
bun install
bun run rust # compile every chapter's Rust to JavaScript with rust-js
bun run test # each chapter's tests, then native Rust's, then
# chapter 23's Rust tests as JavaScript
To run one chapter's comparison: bun chapters/01-the-same-function-twice/compare.js.
coming-from-javascript/
├── vendor/rust-js/ the compiler, a submodule pinned to a commit
├── rust-js/crates/ crates.io crates rust-js compiled (chapter 21)
├── chapters/01-the-same-function-twice/
│ ├── js/stats.js the JavaScript
│ ├── rust/src/lib.rs the Rust
│ ├── rust/src/lib.js what rust-js writes from it (committed)
│ ├── rust/tests/native.rs native Rust on the same cases
│ ├── cases.json the inputs, and what each should give
│ └── compare.js, .test.js both, side by side
├── scripts/patch-rust-js.ts applies patches/, while any wait upstream
└── PATCHES.md what's patched, and why
rust-js also writes a .d.ts beside each lib.js, TypeScript's
description of it. It's committed too, and your editor reads it to
autocomplete the Rust's functions from JavaScript.
If a chapter says something rust-js doesn't do, or does differently, open an issue on rust-js or here.