Generic Constraints
泛型约束
What you’ll learn: How trait bounds and
whereclauses compare to Java generic bounds.
本章将学习: trait bound 和where子句分别怎样对应 Java 的泛型约束。Difficulty: 🟡 Intermediate
难度: 🟡 中级
Java developers know bounds such as <T extends Comparable<T>>. Rust expresses similar ideas through trait bounds.
Java 开发者很熟悉 <T extends Comparable<T>> 这种写法。Rust 则通过 trait bound 表达类似意思。
#![allow(unused)]
fn main() {
fn sort_and_print<T: Ord + std::fmt::Debug>(items: &mut [T]) {
items.sort();
println!("{items:?}");
}
}
The same bounds can be moved into a where clause for readability:
如果约束太长,也可以挪进 where 子句提升可读性:
#![allow(unused)]
fn main() {
fn sort_and_print<T>(items: &mut [T])
where
T: Ord + std::fmt::Debug,
{
items.sort();
println!("{items:?}");
}
}
Key Difference from Java
和 Java 的关键差异
Rust bounds are closely tied to behavior required by the compiler and standard library traits. They are not just nominal inheritance constraints.
Rust 里的约束更紧密地绑定在行为能力上,通常是编译器和标准库 trait 真正需要的能力,而不只是名义上的继承关系。
Advice
建议
- use inline bounds for short signatures
签名短时直接写内联约束。 - use
whereclauses when bounds become long
约束一长,就改用where。 - think in capabilities, not class hierarchies
思考方式优先放在“能力”,不是类层级。