Hacker News new | ask | show | jobs
by puffoflogic 1241 days ago
Here's one notable use that I doubt many are familiar with:

    let iter = some_iterator_expression;
    {iter}.nth(2)
For some reason, nth takes &mut self instead of self (note that T: Iterator => &mut T: Iterator<Item=T::Item> so there was no need for this in the API design to support having access to the iterator after exhaustion; it was a mistake). So if you tried to use it like iter.nth(2) with the non-mut binding, that would fail. But {iter} forces iter to be moved - unlike (iter) which would not. Then iter becomes a temporary and we're free to call a &mut self method on it.

In general: {expression} turns a place expression into a value expression, i.e. forcing the place to be moved from.

1 comments

Interesting. I don't see any use for it (since adding `mut` to the `let iter` declaration sounds less annoying than dropping `iter` altogether) but I learned something nonetheless, thank you.