Logo

Programming-Idioms

  • Python
  • Rust

Idiom #258 Convert list of strings to list of integers

Convert the string values from list a into a list of integers b.

let b: Vec<i64> = a.iter().map(|x| x.parse::<i64>().unwrap()).collect();

a has type Vec<&str>
let b: Vec<i32> = a.iter().flat_map(|s| s.parse().ok()).collect();

ok converts a Result to an Option. flat_map collects all values of Some.
b = [*map(int, a)]
b = [int(elem) for elem in a]
System.Linq;
var b = a.Select(i => int.Parse(i)).ToList();

New implementation...