Eliminating the prelude glob import from Rust library APIs
The article explains why some Rust crates ask users to write `use xxx::prelude::*` at the top of every file. Many libraries split a type's methods into fine-grained extension traits such as CompareExt, TextExt and NumericExt. Rust only lets you call a trait method when that trait is in scope, so `Var<String>` cannot call `.starts_with()` without the import.
The author says the prelude glob import[1] creates new problems. When several crates are imported this way, common method names such as .filter() and .get() collide, reviewers cannot see where a method comes from, and the Methods section on docs.rs is often empty.
The proposed fix is to define every public method directly on the struct as an inherent method[2], and to use traits only as compile-time limits in a where[3] clause. Under this design, `Var<T>` gets .add() only when T is numeric and .starts_with() only when T is text.
Read the original edvong.comDiscussion
Discussion
Most commenters say the glob import is a rare, small cost that is worth the convenience, and one notes that a prelude module with aliases helps. Others dislike glob imports because they hide where a symbol comes from when you search a file. One commenter asks how a type with no constraints on T could do string operations at all.
Sources
-
Edmond Vong
How to Eliminate "use xxx::prelude::*"


