Hacker News new | past | comments | ask | show | jobs | submit login

Cool list! I learned a lot of new things, kudos.

One suggestion: where you talk about using AsRef<[T]>, might be worth suggesting IntoIter, where it makes sense? IIRC that's what std uses for functions that aim at being generic over data structures and iterators (I might be remembering wrong, though).

In any case let's not digress from the truth: amazing list.




There are a few of these conversion traits (for turning some kind of Thing into a Thing with a different purpose)

You're thinking of IntoIterator, which is a trait for when you want an Iterator, maybe what you've got is already an iterator, but maybe it isn't yet and yet it could easily be transformed into one (like say an array), so in either case IntoIterator::into_iter() gets you an iterator instead of whatever you have now -- the thing you had before is consumed by this.

Another you may find uses for is ToString, which says this obviously could be a string and I want the string. Unlike IntoIterator this does not consume the thing you wanted a string for.

There are also a suite of generic traits for converting to another type, From, Into, TryFrom and TryInto. As the person consuming things, prefer TryInto if you're clear what should happen when you can't, or Into if you need it to always work. As the person who is implementing the traits, prefer to implement From or if you can't TryFrom. Rust automatically cascades the conversions in one direction, so if I implemented From<Goose> for Bird, and then you later write code which needs TryInto<Bird>, that'll work just fine with a Goose using the code I wrote, because Rust concludes how to use my conversion to make it work, automatically - Rust will even be able to see that your "What if I can't?" error handling code will never run, and optimise it out of production code since I provided From<Goose> so this cannot fail - its Error type is Infallible, an Empty type.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: