Rust for TypeScript Developers
If youβre a frontend dev exploring Rust, youβll love its emphasis on type safety, performance, and tooling.
π€ Similarities
- Strong static typing
- Good tooling (e.g. Cargo = npm)
- Expressive enums and pattern matching
π§ Differences
- Ownership model
- No garbage collection
- Compilation times (slower, but worth it)
β¨ Hello World in Rust
fn main() {
println!("Hello, TypeScript dev!");
}
π¦ Project Setup
cargo new hello-rust
cd hello-rust
cargo run
π§ Borrowing vs. Ownership
fn greet(name: &str) {
println!("Hello, {}", name);
}
You pass by reference using &str, not by value. Memory safety is enforced at compile time.
π§ͺ Enums vs. Discriminated Unions
Rust:
enum Role {
Admin,
User,
Guest,
}
TypeScript:
type Role = "Admin" | "User" | "Guest";
π Error Handling
Rust uses Result<T, E> instead of exceptions:
fn divide(a: f64, b: f64) -> Result<f64, String> {
if b == 0.0 {
Err("Cannot divide by zero".into())
} else {
Ok(a / b)
}
}
π Conclusion
Rust has a steep learning curve, but pays off long-term.
If TypeScript is your safety net, Rust is your force field.