Kick off your book project in 2 hours, get started with GhostAI in 2 hours, or do both! Free live workshops, on Zoom. You’ll leave with a real book project and a clear plan to keep going. Saturday, June 27, 2026.

Leanpub Header

Skip to main content

Rust for Javascript Developers

A Practical Guide to Systems Programming for the Web Developer

This book is 100% completeLast updated on 2026-06-19

Learn Rust the fast way, by mapping it to the JavaScript you already know. Go from ownership and traits to building a CLI, an Axum web server, a SQLx database layer, and WebAssembly modules you can call straight from JavaScript.

Minimum price

$9.00

$15.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
About

About

About the Book

You already know JavaScript. This book teaches you Rust by building on it.

Every new idea is introduced through its JavaScript or TypeScript equivalent, so you always have a familiar anchor: closures stay closures, Promises become Futures, interfaces become traits, and npm becomes Cargo. From there you tackle the parts that make Rust different, like ownership and borrowing, a strict type system, error handling with Result and the ? operator, and fearless concurrency, all at a pace that respects the experience you already have.

It's a hands-on book. You'll set up your toolchain, work through the fundamentals, then build real things: a command-line tool, a web server with Axum, a database layer with SQLx, and serialisation with Serde. The final part is where your JavaScript background becomes a genuine advantage. You'll compile Rust to WebAssembly and call it from JavaScript to speed up the work JavaScript struggles with.

Inside, you'll find:

• The whole language: types, ownership, traits, generics, smart pointers, iterators, and closures, each mapped to its JS/TS counterpart.

• Real projects, not toy snippets: a CLI, a web API, database access, and WebAssembly modules.

• Honest advice on when Rust is the right tool, and when it isn't.

• Current, working code using today's Rust toolchain and the crates the community actually uses.

You don't need any systems-programming background, and you don't need C or C++. If you're comfortable with JavaScript and curious about Rust, this is your bridge.

Share this book

Author

About the Author

Dwayne Charrington

Dwayne is a front-end developer with over 10 years experiencing in the web development industry. Initially starting out as a full-stack developer before switching to a front-end developer a few years into his career, he enjoys writing about Javascript and has a deep passion for the web.

Contents

Table of Contents

Rust for JavaScript Developers

  1. A Practical Guide to Systems Programming for the Web Developer

Introduction

  1. Who This Book Is For
  2. What This Book Covers
  3. How to Read This Book
  4. A Note on Code Examples
  5. Let’s Get Started

Part I: Getting Started

Why Rust?

  1. What Rust Offers That JavaScript Does Not
  2. Where Rust Fits in the Software Landscape
  3. Why JavaScript Developers Are Well-Positioned to Learn Rust
  4. The Learning Curve Is Real
  5. What This Book Will Not Do

Setting Up Your Environment

  1. Installing Rust with rustup
  2. Tour of the Rust Toolchain
  3. Editor Setup
  4. Your First Rust Program
  5. Project Structure as It Grows
  6. Summary

Thinking in Rust: A Mental Model Shift

  1. Interpreted vs Compiled
  2. Dynamic Typing vs Static Typing
  3. Garbage Collection vs Ownership
  4. The Compiler as Your Pair Programmer
  5. A Side-by-Side: The Same Program
  6. What to Expect Going Forward

Part II: The Language Fundamentals

Variables, Types, and Functions

  1. Variables and Mutability
  2. Scalar Types
  3. Type Inference
  4. Type Annotations
  5. Functions
  6. Shadowing
  7. Constants and Statics
  8. Tuples
  9. Arrays
  10. Quick Reference: JS Types to Rust Types
  11. Summary

Strings and Text

  1. Why Strings Are More Complex in Rust
  2. String vs &str
  3. Creating Strings
  4. Concatenation
  5. Common String Operations
  6. Converting Between String and &str
  7. The format! Macro
  8. UTF-8 Encoding
  9. Practical Patterns
  10. Summary

Ownership and Borrowing

  1. The Three Ownership Rules
  2. Move Semantics
  3. Borrowing with References
  4. The Borrowing Rules
  5. Lifetimes
  6. Practical Patterns
  7. Common Ownership Errors and How to Fix Them
  8. How This Compares to JavaScript
  9. Summary

Control Flow and Pattern Matching

  1. if/else Expressions
  2. Loops
  3. Pattern Matching with match
  4. if let: Concise Pattern Matching
  5. while let: Loop Until Pattern Fails
  6. let-else: Handle the Failure Case
  7. Destructuring: Beyond match
  8. Comparison: JS Destructuring vs Rust Pattern Matching
  9. Summary

Structs and Implementations

  1. Defining Structs
  2. Creating Instances
  3. Mutability
  4. Tuple Structs
  5. Unit Structs
  6. The impl Block: Adding Methods
  7. The Builder Pattern
  8. Deriving Common Traits
  9. Printing and Displaying Structs
  10. Comparison: JS Classes vs Rust Structs
  11. Summary

Enums and Option/Result

  1. Basic Enums
  2. Enums with Data
  3. Methods on Enums
  4. The Option Enum: Rust’s Answer to null
  5. The Result Enum: Rust’s Answer to try/catch
  6. Chaining with map, and_then, unwrap_or
  7. Converting Between Option and Result
  8. Practical Example: Parsing a Config File
  9. Comparison: JS Error Handling vs Rust
  10. Summary

Collections

  1. Vec: Dynamic Arrays
  2. HashMap: Key-Value Maps
  3. HashSet: Unique Collections
  4. BTreeMap and BTreeSet
  5. Choosing the Right Collection
  6. Practical Example: Word Frequency Counter
  7. Quick Reference: JS Collections vs Rust Collections
  8. Summary

Iterators and Closures

  1. Closures
  2. Iterators
  3. Building Custom Iterators
  4. Practical Example: Data Pipeline
  5. Quick Reference: JS Array Methods vs Rust Iterators
  6. Summary

Part III: The Type System in Depth

Traits

  1. Defining a Trait
  2. Implementing a Trait
  3. Default Method Implementations
  4. Traits as Parameters
  5. Trait Bounds
  6. Common Standard Library Traits
  7. Trait Objects and Dynamic Dispatch
  8. Returning Traits from Functions
  9. Implementing External Traits for Your Types (and Vice Versa)
  10. Comparison: TypeScript Interfaces vs Rust Traits
  11. Summary

Generics

  1. Why Generics
  2. Generic Functions
  3. Generic Structs
  4. Generic Enums
  5. Generic Implementations
  6. Constraining Generics with Trait Bounds
  7. Multiple Trait Bounds
  8. Monomorphization: Zero-Cost Generics
  9. Turbofish Syntax
  10. Common Patterns
  11. Comparison: TypeScript Generics vs Rust Generics
  12. Summary

Error Handling in Depth

  1. Designing Error Types
  2. The thiserror Crate
  3. The anyhow Crate
  4. Error Propagation Patterns
  5. When to Panic vs When to Return an Error
  6. Practical Example: A CLI Application’s Error Handling
  7. Comparison: JS Error Handling vs Rust Error Handling
  8. Summary

Smart Pointers and Interior Mutability

  1. Box: Heap Allocation
  2. Rc: Reference Counting
  3. Arc: Atomic Reference Counting
  4. RefCell: Interior Mutability
  5. Cell: Interior Mutability for Copy Types
  6. Mutex: Thread-Safe Interior Mutability
  7. Weak: Breaking Reference Cycles
  8. Choosing the Right Smart Pointer
  9. Comparison to JavaScript
  10. Summary

Part IV: Organising and Scaling Rust Code

Modules, Crates, and Packages

  1. The Hierarchy
  2. Modules
  3. File-Based Module Structure
  4. Crates: Library and Binary
  5. Workspaces: Monorepos
  6. Comparison: JS Modules vs Rust Modules
  7. Summary

Cargo in Depth

  1. Cargo.toml: The Manifest
  2. Semantic Versioning
  3. Adding Dependencies
  4. Features: Conditional Compilation
  5. Build Profiles
  6. Build Scripts
  7. Publishing to crates.io
  8. Useful Cargo Subcommands
  9. Cargo Workspaces for Monorepos
  10. Comparison: package.json vs Cargo.toml
  11. Summary

Testing

  1. Unit Tests
  2. Assertion Macros
  3. Testing for Panics
  4. Tests That Return Result
  5. Ignoring Tests
  6. Test Organisation
  7. Documentation Tests
  8. Test Filtering and Running
  9. Mocking and Test Doubles
  10. Property-Based Testing with proptest
  11. Benchmarking with criterion
  12. Comparison: Jest/Vitest vs Rust Testing
  13. Summary

Part V: Concurrency and Async

Fearless Concurrency

  1. Threads
  2. The Send and Sync Traits
  3. Shared State with Mutex and Arc
  4. Message Passing with Channels
  5. Rayon: Easy Data Parallelism
  6. Common Concurrency Patterns
  7. Comparison: JS Concurrency vs Rust Concurrency
  8. Summary

Async Rust

  1. async/await: The Familiar Parts
  2. Futures vs Promises
  3. The Tokio Runtime
  4. Spawning Async Tasks
  5. Selecting Between Futures
  6. Async Channels
  7. Common Pitfalls
  8. Streams: Async Iterators
  9. Practical Example: Concurrent HTTP Requests
  10. Comparison: JS Async vs Rust Async
  11. Summary

Part VI: Real-World Rust

Building a CLI Application

  1. Project Setup with Cargo
  2. Understanding Clap and Argument Parsing
  3. Reading Files
  4. Implementing the Search Logic
  5. Coloured Terminal Output
  6. Error Handling and User-Friendly Messages
  7. Working with stdin and stdout
  8. Complete minigrep Implementation
  9. Key Differences from Node.js
  10. Building for Distribution
  11. Exercises

Building a Web Server

  1. The Rust Web Ecosystem
  2. Why Axum?
  3. Routing and Handlers
  4. JSON Serialization and Deserialization
  5. Middleware and Layers
  6. Shared State with Arc
  7. Full Project: Building a Task Manager REST API
  8. Advanced Extractors
  9. Error Handling
  10. Comparison: Axum vs. Express/Fastify
  11. Performance Considerations
  12. Deploying a Web Server
  13. Wrapping Up

Working with Databases

  1. The Rust Database Ecosystem
  2. Why SQLx Stands Out
  3. Setting Up SQLx
  4. Connecting to Databases
  5. Migrations with sqlx-cli
  6. Query Building and Execution
  7. Advanced Queries
  8. Transactions
  9. Connection Pooling
  10. Mapping Rows to Structs
  11. Inserting and Updating Data
  12. Compile-Time Query Verification
  13. Comparing to JavaScript ORMs
  14. Advanced Patterns
  15. Database-Specific Features
  16. Error Handling
  17. Summary

Serialization and Deserialization with Serde

  1. Why Serde is Everywhere
  2. The Fundamentals: Derive Serialize and Deserialize
  3. Working with JSON: serde_json
  4. Working with Other Formats: TOML and YAML
  5. Customising Serialization with Attributes
  6. Custom Serialization and Deserialization
  7. Handling Optional Fields
  8. Working with Enums
  9. Advanced Patterns
  10. Serde Attributes: Quick Reference
  11. Real-World Example: API Client
  12. Comparison with JavaScript
  13. Conclusion

Part VII: Rust and WebAssembly

Introduction to WebAssembly

  1. What is WebAssembly?
  2. Why Rust developers should care
  3. How WebAssembly Works
  4. When to use WebAssembly (and when not to)
  5. The Rust-to-Wasm Pipeline
  6. Tools Overview
  7. A Quick Example: Hello World in Wasm
  8. Performance Characteristics
  9. Next Steps

Building Wasm Modules with Rust

  1. Setting Up a Rust Wasm Project
  2. Writing Wasm Functions with wasm-bindgen
  3. Exposing Different Data Types to JavaScript
  4. Working with the DOM from Rust
  5. Calling JavaScript from Rust
  6. Handling Errors Across the Boundary
  7. Debugging Wasm Modules
  8. Building a Full Project: High-Performance Text Processing Module
  9. Performance Considerations
  10. Summary

Integrating Wasm into JavaScript Projects

  1. Loading Wasm in the Browser
  2. Loading Wasm in Node.js
  3. Bundler Integration
  4. Performance Profiling: When is Wasm Faster?
  5. Memory Management Across the Boundary
  6. Packaging and Distributing Wasm Modules
  7. Real-World Patterns and Best Practices
  8. Summary Comparison Table

Part VIII: Advanced Topics

Unsafe Rust

  1. Understanding Unsafe
  2. What You Can Do in Unsafe Blocks
  3. Raw Pointers
  4. Unsafe Functions and Methods
  5. Mutable Statics
  6. Unsafe Traits
  7. Union Fields
  8. When Unsafe is Justified
  9. Writing Safe Abstractions Over Unsafe Code
  10. Guidelines for Unsafe Code
  11. Unsafe in Practice

Macros

  1. What Are Macros and Why Does Rust Use Them So Heavily?
  2. Declarative Macros with macro_rules!
  3. Writing Your Own Declarative Macros
  4. Procedural Macros: An Overview
  5. Common Macro Patterns in the Ecosystem
  6. When to Use Macros vs. Generics vs. Traits
  7. Debugging Macros with cargo expand
  8. Comparing to JavaScript Metaprogramming
  9. Practical Advice: Most People Use Macros More Than They Write
  10. Summary

Performance and Optimisation

  1. Debug Mode vs Release Mode
  2. Release Profile Configuration
  3. Profiling: Understanding Where Time Goes
  4. Common Performance Patterns
  5. Compiler Optimisation Hints
  6. Binary Size Reduction
  7. Rust Ahead-of-Time vs JavaScript’s JIT Model
  8. Practical Optimisation Workflow
  9. When to Care About Performance
  10. Summary

Appendix A: Rust Cheat Sheet for JavaScript Developers

  1. Variables and Types
  2. Functions
  3. Control Flow
  4. Error Handling
  5. Strings
  6. Collections and Iterators
  7. Structs and Classes
  8. Modules and Imports
  9. Async
  10. Common Patterns
  11. Operator Quick Reference

Appendix B: Cargo Commands Reference

  1. Project Setup
  2. Building
  3. Dependencies
  4. Testing
  5. Code Quality
  6. Publishing
  7. Workspace Commands
  8. Useful Third-Party Cargo Subcommands
  9. Environment Variables
  10. Quick Comparison: npm vs Cargo

Appendix C: Recommended Crates

  1. Serialization and Data Formats
  2. Web and HTTP
  3. Async Runtime
  4. Database
  5. Command-Line Tools
  6. Error Handling
  7. Logging and Tracing
  8. Testing
  9. Date, Time, and UUIDs
  10. Cryptography and Security
  11. Configuration
  12. WebAssembly
  13. Concurrency and Parallelism
  14. File and Path Handling
  15. Regular Expressions
  16. Choosing Crates

Appendix D: Further Resources

  1. Official Resources
  2. Interactive Learning
  3. Intermediate and Advanced
  4. WebAssembly
  5. Community
  6. Blogs and Video
  7. Books
  8. Staying Current

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub