Want for intermediate Rust book / docs: a clean cut tutorial on how to implement the Iterator trait. Took a while to figure out that for many cases we need to create a secondary struct that holds / borrows the primary struct and adds a counter / lifetimes. This isn't documented.
-
Show this thread
-
I think a good place to start with is: "Why can we iterate over some structs in Rust's stdlib, whereas for others we need to call `.iter()` before we can iterate over them?" This might be obvious once you understand it, but it can be hard to find out without digging into the src
1 reply 0 retweets 1 likeShow this thread -
yosh Retweeted yosh
Using the terms I wrote about before: - entry level: learn what iterators are and how to use them. - intermediate: learn how to implement iterators for your own types. - seasoned: not sure, haven't hit this point yet.https://twitter.com/yoshuawuyts/status/1006654124505681921 …
yosh added,
yosh @yoshuawuytsI think a lot of "Intermediate Rust" is centered around constructing ergonomic APIs. I think Rust's progression is somewhat like this: - Entry level: learn how to do things. - Intermediate: learn how to do things cleanly. - Seasoned: expand the boundaries of what can be done.Show this thread2 replies 0 retweets 2 likesShow this thread -
Replying to @yoshuawuyts
Efficient ways to iterate and think about workload, possibly using rayon for parallelism if possible for seasoned would be one thing. This requires knowing how to profile your rust code to see if that iterator is even a hot spot for example
1 reply 0 retweets 0 likes -
Replying to @mgattozzi
Oh yeah, that's a good one! Introducing parallel iterators (Rayon) / Future Streams might be at the edge of what's possible. Another one I'm thinking of is overloading the built-in trait implementations to tune them for performance. Yay, yeah that makes sense! :D
1 reply 0 retweets 1 like -
Replying to @yoshuawuyts
Oh like overloaded traits for std types? Because Rust does not have specialization (yet soundness issues) so you'd have to do some type system trickery or cerate a wrapper type to get that.
1 reply 0 retweets 0 likes -
Replying to @mgattozzi
More like replacing the default implementations. For example `Iterator.last()` can easily be optimized to not do a full traversal of the structure. There's probably a lot of these around. https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.last …
1 reply 0 retweets 1 like -
Replying to @yoshuawuyts
Oh yeah you only have to provide next for iterator but you can make your own version of the method for the trait implementation now that I'm thinking about it! Yeah this definitely then :D
1 reply 0 retweets 1 like -
Replying to @mgattozzi @yoshuawuyts
if you are looking for a blog post to write, please do one that explains std::iter::ExactSizeIterator and friends (I know it exists and have a feeling that it's awesome but have otherwise no idea)
1 reply 0 retweets 3 likes
Haha, yeah it'd be nice to know what it does. Only thing I know is that it has .len() and .is_empty(), but idk when to use it :p
-
-
Replying to @yoshuawuyts @killercup
When you know the exact size! So if you know you'll always have a list of size 10 at a given point then this would be a more efficient iterator trait to implement. If you know the len then you can call len() whereas iterator has to traverse the whole thing to get a size.
1 reply 0 retweets 1 like -
I bet it also provides hints to the compiler so it could do things like loop unrolls and stuff but I have no proof on that one.
0 replies 0 retweets 1 like
End of conversation
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.