why are there two different modules for &str
and &[u8]
searching instead of one generic API?
#1075
-
Originally asked by @johnmcilwain: Hi, (rust beginner here) - hope this isn't a dumb question :-) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
This is a good question. The short answer is that I personally find that generics makes APIs more complex, both in terms of understanding and for use. For use is easy to answer: it can negatively impact inference. It's much nicer, for example, to accept an There is also a longer answer which is that at a technical level, it's actually a little trickier than you might imagine to craft a single generic API that is convenient to use. I elaborated a little bit on this in the I think the bottom line here is that, in my experience, the I want to be very clear that reasonable people can disagree here, and the API design I chose reflects a fair bit of subjective judgment on my part that tries to balance a lot of things:
And probably other unstated things as well. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the info. In my case, I ended up using regex::bytes, but I really don't know why I ended up with it. |
Beta Was this translation helpful? Give feedback.
This is a good question. The short answer is that I personally find that generics makes APIs more complex, both in terms of understanding and for use. For use is easy to answer: it can negatively impact inference. It's much nicer, for example, to accept an
&str
instead of anAsRef<[u8]>
. For understanding, it's a bit more subtle and reasonable people can disagree. But in my experience, the existence of type parameters requires reasoning through an abstraction instead of reasoning concretely with specific types. For something like theregex
crate, I tried hard to optimize for ease of understanding due to how many folks of varying experience levels are likely to use the crate.regex
might b…