Ask anything about this article
Hi! I've read this article.
What would you like to know?
@farhan
Rust is eating the JavaScript ecosystem from the inside. Turbopack (Next.js bundler), SWC (Babel replacement), Biome (ESLint replacement), Deno, and even parts of Node.js are being rewritten in Rust. If you're a JavaScript developer, Rust is the most impactful second language you can learn.
rustfn main() {
let name = "World";
println!("Hello, {}!", name);
}
In Rust, variables are immutable by default:
rustlet x = 5; // Immutable
// x = 6; // ❌ Compile error!
let mut y = 5; // Mutable
y = 6; // ✅ Works
This is the opposite of JavaScript where let is mutable by default.
Rust's most unique feature is its ownership system:
rustfn main() {
let s1 = String::from("hello");
let s2 = s1; // s1 is "moved" to s2
// println!("{}", s1); // ❌ Compile error! s1 is no longer valid
println!("{}", s2); // ✅ Works
}
javascriptlet s1 = "hello";
let s2 = s1; // Both s1 and s2 point to the same data
console.log(s1); // ✅ Works (JavaScript copies references)
Instead of moving ownership, you can "borrow" a reference:
rustfn calculate_length(s: &String) -> usize {
s.len()
} // s goes out of scope, but since it's a reference, nothing is dropped
fn main() {
let s = String::from("hello");
let len = calculate_length(&s); // Borrow s
println!("Length of '{}' is {}", s, len); // ✅ s is still valid
}
Rust's match is like a supercharged switch:
rustenum Shape {
Circle(f64),
Rectangle(f64, f64),
Triangle(f64, f64, f64),
}
fn area(shape: &Shape) -> f64 {
match shape {
Shape::Circle(r) => std::f64::consts::PI * r * r,
Shape::Rectangle(w, h) => w * h,
Shape::Triangle(a, b, c) => {
let s = (a + b + c) / 2.0;
(s * (s-a) * (s-b) * (s-c)).sqrt()
}
}
}
rustuse std::fs;
fn read_file(path: &str) -> Result<String, std::io::Error> {
fs::read_to_string(path)
}
fn main() {
match read_file("data.txt") {
Ok(content) => println!("File: {}", content),
Err(e) => eprintln!("Error: {}", e),
}
// Or use the ? operator for concise error propagation
let content = fs::read_to_string("data.txt")?;
}
| JavaScript Tool | Rust Replacement | Speed Improvement |
|---|---|---|
| Webpack | Turbopack | 10x faster |
| Babel | SWC | 20x faster |
| ESLint + Prettier | Biome | 35x faster |
| Node.js (partial) | Deno / Bun | 2-5x faster |
bash# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Create a new project
cargo new my-project
cd my-project
cargo run
Rust has a steep learning curve (especially ownership and lifetimes), but it fundamentally changes how you think about software. The skills transfer back to JavaScript, making you a better programmer overall.