2025-06-03
Rust struct vs TypeScript interface: What's the Difference?
Both Rust structs and TypeScript interfaces describe data shapes, but they work very differently. Here's what you need to know.
The Surface Similarity
Both struct in Rust and interface in TypeScript describe the shape of your data:
TypeScript:
export interface User {
id: number;
name: string;
}
Rust:
#[derive(Debug, Serialize, Deserialize)]
pub struct User {
pub id: i64,
pub name: String,
}
They look similar, but the implications are profoundly different.
Memory and Ownership
A TypeScript interface is a compile-time fiction — it disappears at runtime. The underlying JavaScript object is heap-allocated with no ownership semantics.
A Rust struct is a concrete memory layout. The compiler knows the exact byte size at compile time. Stack-allocated structs are possible and common.
Nullability
TypeScript uses | null or optional fields (?:). Rust uses Option<T>. Both force you to handle the absent case, but Rust's match on Option is exhaustive — the compiler won't let you ignore it.
Which to Use When?
| Scenario | Pick |
|---|---|
| Frontend web app | TypeScript interface |
| High-performance API | Rust struct |
| Microservice | Go struct |
| .NET ecosystem | C# record/class |
The right tool depends on your runtime, not your data shape.