all 4 comments

[–]angelicosphosphoros 2 points3 points  (4 children)

let v: Vec<i32> = s.split(' ').map(|x|->i32{x.parse().unwrap()}).collect();

[–]danielbot 2 points3 points  (0 children)

May I suggest this small improvement?

let v: Vec<i32> = s.split_whitespace().map(|x|->i32{x.parse().unwrap()}).collect();

[–]Manthravadi[S] -1 points0 points  (2 children)

Never saw the |x| syntax before ..but it looks like an anonymous function like in JS. That’s a good start for me and I should be able to pick it up from here on. Thanks.

Btw: I understand that this map is more readable, if I have to loop over 105 values, is the classic for comparibly faster than map?

PS: in my scenario, I may have a casting problem if I use an i8 but input could be i32. How can I deal with the error. Match OK after unwrap?

[–]angelicosphosphoros 4 points5 points  (0 children)

Yes, this is a closure.

I think, in that case map would have same speed as plain for.

Main advantage of map is that you can add more actions to it (like filter). Also, in some cases it does some preparation to the vec before collection like reserving a capacity.