3 ms·
fn func (slice: &[impl AsRef<str>]) { // ... }
by hctaw 6y ago
fn func (slice: &[impl AsRef<str>]) {
// ...
}
- howinteresting 6y ago* You have to remember to do that. * I was thinking return values. * Also you can't use that style in an enum definition if you want to return a custom enum.
- hctaw 6y agoIf you need api flexibility you use generics, and that is the way to be generic over types that refer to strs. I'm pretty sure this is in the book, and it's common enough that even someone who doesn't use rust full time (myself) knows it off the top of their head You can use impl Trait in returns, this is actually one of the reasons why that feature exists. And yes, you can use generics in an enum.
- howinteresting 6y agoYou have to expose the generic in the enum though. Rust doesn't have existential types at the moment.
- pornel 6y agoI think it's a valid criticism. Ownership, mutability, and thread-safety are not easy to abstract over and hide as implementation details in Rust. It's a side effect of the fact that Rust actually cares about these and checks them strictly, but for users coming from eg Java that's a bit of a shock.