Compact closure args

I didn’t know you could do that. I really like this syntax, but it works out nicer in Python where you don’t have to worry about passing references or values. A common case (for me) is passing in the first parameter by value (like an i32, for example). In this case, this syntax fails. So I can’t do simple things like let v2: Vec<i32> = v.iter().map(double).collect() (where double doubles an element) without modifying the double function itself. Really annoying.

You can add a deref map into your iter line. v.iter().map(|i| *i).map(double)...

Yea, but that at that point I might as well write v.iter().map(|i| double(*i)).collect().

v.iter().cloned().map(double).collect()

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.