It’s mostly to make callers more ergonomic. A better example is APIs that take
AsRef<Path> - this allows callers to use strings, string slices,
PathBuf and whatever else implements that trait. The caller can do the conversion themselves of course but this adds noise to all the callsites. Similarly, taking
Into<B> allows caller to pass different types that can convert into
B without conversion noise.
The major downside is code bloat - every invocation of the method with a different type causes specialization for that type to be generated (monomorphization). This is good in that compiler can optimize better but it can lead to lots of versions of the method being generated in the machine code. Consider also that this cascades - if you continue to call generic methods in the chain, they’ll also get specialized versions. This also increases compilation time. A common technique to combat that is to provide a public API that exposes the generic function, but then use the underlying type internally from there on out (this is best illustrated with the
The second downside is generics-heavy code is harder to read and understand, on average.