[rust-dev] The 'for' syntax for iterators

Noam Yorav-Raphael noamraph at gmail.com
Thu Jun 13 07:17:22 PDT 2013


Hello,

I want to point something that may be a reason for changing the 'for'
syntax for external iterators: list comprehensions.

I find list comprehensions (and generator expressions) in Python to be
something that makes Python code much more concise, and make Python much
more fun for data exploration. For example, without generator expression,
I'd have to write instead of this:

print mean(person.height for person in people if person.weight > 30)

something like this:

L = []
for person in people:
    if person.weight > 30:
        L.append(person.height)
print mean(L)

With rust moving to external iterators, it seems to me that adding list
comprehensions is entirely possible. I'm not saying it should be done
before 1.0, but it may be worthwhile to prepare for that.

If the current syntax is preserved, the list comprehension should look
something like this, to preserve consistency:

mean(person.height for people |person| if person.weight > 30)

and even

mean(person.height for families |family| for family |person| if
person.weight > 30)

I find it much less readable than

mean(person.height for family in families for person in family if
person.weight > 30)

My point is that the choice of syntax may have consequences for more syntax.

I'd be very happy to write a script to convert from "for y |x|" to "for x
in y", as I believe it would make rust significantly more easy to read and
write (I believe it shouldn't be difficult given a tokenizer)

One another point for switching the syntax: It may actually help the
transition to external iterators. Have 'for x in y' handle only for
external iterators and 'for y |x|' only internal iterators. Gradually
replace all internal iterators by external ones, and when finished, remove
the internal 'for' syntax from the language.

I hope you'll pardon me for giving my opinion without being a developer - I
just find rust really exciting, and would like it to be as perfect as it
could be.

Thanks for reading this, and have a great day,
Noam
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20130613/8ebc3db3/attachment.html>


More information about the Rust-dev mailing list