Le 10/08/2013 15:56, Jordi Boggiano a écrit :
On 10.08.2013 16:10, Simon Sapin wrote:
>Proposal:
>
>     for i in iter {
>         // ...
>     } else {
>         // ...
>     }
>
>The optional else block is executed when the for loop stops without
>exhausting the iterator, ie. when "break" is used.
Maybe that's common to python devs, but I would expect else to be
executed if the iterator yielded nothing/was empty.

I agree that would also be useful, but I don’t know how to have both.

It is what Jinja2 is doing. (A templating language for Python.) For what it’s worth, I find else-on-empty especially useful in "templates" (ie. generating stuff based on some data) and else-on-exhaustion especially useful in "data-manipulation" code.


IMO if you break you'll already be in a conditional of some sort in the
for body, so you can add more code there.

Yes, it’s trivial to do stuff when you break. The trick is doing stuff when you *don’t*. Without this proposal, the options are:

* Using a boolean flag, flipped when you break
* Avoiding the for loop entirely, using iter.next(), and doing stuff when receiving None.

… none of which is great.

--
Simon Sapin
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to