跳到正文

写给 JavaScript 开发者的 Rust:值得迈出的那一步

本页目录

Rust 好几年前就进入了 Web 开发者的视野,但采用速度一直不快。2026 年,局势变了:JS 生态里的关键工具都由 Rust 驱动(Biome、Oxc、Rolldown、SWC 编译器),再加上 WebAssembly,它在前端也变得不可或缺。是时候学它了。

最大的思维转变:所有权

在 JavaScript 里,内存由垃圾回收器管理。在 Rust 里,这份责任通过所有权系统交给了编译器。

// In JS: this works
// let a = [1, 2, 3];
// let b = a; // a is still valid

// In Rust:
fn main() {
    let a = vec![1, 2, 3];
    let b = a;          // a is "moved" to b
    println!("{:?}", a); // ✗ ERROR: a was moved
    println!("{:?}", b); // ✓
}ownership.rs

解决方案:使用引用进行借用。

fn main() {
    let a = vec![1, 2, 3];
    let b = &a;          // immutable borrow
    println!("{:?}", a); // ✓ a is still valid
    println!("{:?}", b); // ✓
}

fn print_vec(v: &Vec<i32>) { // receives reference, not ownership
    for n in v {
        print!("{} ", n);
    }
}borrowing.rs

类型:从 any 到世界上最安全的类型系统

JavaScript/TypeScript 对应的 Rust
number i32、u32、f64……
string String(堆上)/ &str(切片)
T | null Option<T>
T | Error Result<T, E>
any[] Vec<T>
{ [key: string]: T } HashMap<String, T>
fn divide(a: f64, b: f64) -> Option<f64> {
    if b == 0.0 {
        None   // equivalent to null without the billion-dollar mistake
    } else {
        Some(a / b)
    }
}

fn main() {
    match divide(10.0, 0.0) {
        Some(result) => println!("Result: {result}"),
        None => println!("Division by zero"),
    }
}types.rs

错误处理:Result 就是 Rust 的 Promise

在 JS 里,你用 try/catch 或 Promise 链来处理错误。在 Rust 里,地道的做法是 Result<T, E>:

use std::fs;
use std::io;

// Before: without the ? operator
fn read_config_verbose() -> Result<String, io::Error> {
    let content = match fs::read_to_string("config.toml") { 
        Ok(c) => c,                                            
        Err(e) => return Err(e),                               
    };                                                         
    Ok(content.to_uppercase())
}

// With the ? operator (equivalent to JS await, but for errors)
fn read_config() -> Result<String, io::Error> {               
    let content = fs::read_to_string("config.toml")?;       
    Ok(content.to_uppercase())                               
}errors.rs

闭包与高阶函数

语法不同,但概念完全一样:

fn main() {
    let numbers = vec![1, 2, 3, 4, 5];

    // map + filter + collect (like Array.map + filter in JS)
    let double_evens: Vec<i32> = numbers
        .iter()
        .filter(|&&x| x % 2 == 0)  
        .map(|&x| x * 2)            
        .collect();

    println!("{:?}", double_evens); // [4, 8]
}closures.rs

Rust → WebAssembly:通往前端的那座桥

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn fibonacci(n: u32) -> u32 {
    match n {
        0 => 0,
        1 => 1,
        _ => fibonacci(n - 1) + fibonacci(n - 2),
    }
}lib.rs
# Compile to WASM
wasm-pack build --target web
import init, { fibonacci } from "./pkg/my_project.js";

await init();
console.log(fibonacci(40)); // ~10x faster than pure JS versionmain.js

从哪里开始

  1. The Rust Book——任何语言里最好的官方文档。
  2. Rustlings——在终端里做互动练习。
  3. Rust by Example——通过真实示例来学。
  4. 用 wasm-pack 做点东西,然后在当前的 Web 项目里用起来。

学习曲线确实陡峭,但 Rust 编译器是你所能找到的最好的老师:它的错误信息详尽、准确,而且几乎总会给出解决方案。

上一篇
TypeScript 5.x:真正改变你写法的那些特性
发布日期:
• 2 分钟阅读
推荐阅读
React 19:useActionState、useOptimistic,以及手动 loading 状态的终结
发布日期:
• 2 分钟阅读
100%