Traits and Generics
Trait 与泛型
What you’ll learn: How Rust traits compare to Java interfaces and how Rust generics differ from erased JVM generics.
本章将学习: Rust trait 如何对应 Java interface,以及 Rust 泛型和 JVM 擦除式泛型有什么根本区别。Difficulty: 🟡 Intermediate
难度: 🟡 中级
Traits are the closest Rust concept to Java interfaces, but they sit inside a more powerful type system.
trait 是 Rust 里最接近 Java interface 的概念,但它所在的类型系统更强,也更直接。
Traits vs Interfaces
trait 与接口
#![allow(unused)]
fn main() {
trait Render {
fn render(&self) -> String;
}
}
Traits can define required behavior and default behavior, much like interfaces with default methods.
trait 可以定义必须实现的行为,也可以带默认实现,这点和带 default method 的接口很像。
Generics
泛型
#![allow(unused)]
fn main() {
fn first<T>(items: &[T]) -> Option<&T> {
items.first()
}
}
Rust generics are monomorphized in many cases, which means the compiler often generates concrete machine code per concrete type rather than relying on erased runtime dispatch.
Rust 泛型在很多情况下会做单态化,也就是说编译器会针对具体类型生成具体机器码,而不是像 JVM 那样主要依赖擦除后的运行时分发。
Static vs Dynamic Dispatch
静态分发与动态分发
- generic trait bounds usually mean static dispatch
泛型 trait bound 通常意味着静态分发。 dyn Traitmeans dynamic dispatchdyn Trait则表示动态分发。
This distinction is far more explicit than in typical Java code.
这种区别在 Rust 里写得非常明白,比典型 Java 代码显式得多。
Why Java Developers Should Care
Java 开发者为什么要在意这件事
Java interfaces often coexist with inheritance, reflection, and proxies. Rust traits tend to stay closer to behavior and less tied to framework machinery.
Java interface 经常和继承、反射、代理一起工作;Rust trait 则更接近纯行为抽象,也更少和框架魔法绑死。
Traits and generics are where Rust starts feeling less like “Java without GC” and more like its own language with its own power.
trait 和泛型这块,往往是 Rust 最开始不再像“去掉 GC 的 Java”,而真正显出自己语言气质的地方。