Some C# LINQ Samples to Rust


#1

A way to judge how good the current Rust iterators are is to use them. So I’ve translated some interesting 101 LINQ C# Samples in Rust (the Swift 3 versions too are visible):

Perhaps there are better ways to write some Rust solution.

Some conclusions:

  • indexed_filter(|index, item|) and indexed_map(|index, item|) iterators are sometimes handy in Rust (linq5, linq25, linq27). indexed_take_while and indexed_skip_while seem too much niche to me.
  • A pairs() iterators could be handy in Rust (linq14) (this is not the cartesian product).
  • I’d like a sorted(), that’s usable in iterator chains, that contains a collect::<Vec<_>>() plus sort() and returns the result (linq31).
  • A group() as in D language could be handy.
  • A lazy unique() similar to the D language uniq() that works on sorted sequences could be very handy (linq46, linq73).
  • I think the Rust std library needs union, difference and intersection on sorted iterators (linq48, linq50, linq52).
  • The std library is also missing a way to print a lazy sequence in a very handy way.
  • It’s nice to have a function that returns all the max/min items (linq88). But its design is not easy, it has to scan two times, or to allocate some memory.

Some of those things are already in the itertools crate, but they should be in the standard library.


#2

A neat hack to do sorted without intermediate variables is via BinaryHeap: https://github.com/rust-lang/cargo/blob/8b5aec111926d1d03d2da32dd494e0fff073f870/src/cargo/core/resolver/mod.rs#L982


#3

Would you be willing to write up a short RFC to this effect, with before/after comparisons as well as comparisons to C# and Swift?