From 90a8ca2587fa940c9d5578d6fbe793965284ee70 Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Wed, 7 Dec 2016 12:45:29 +0100 Subject: [PATCH] Add code from the lecture --- materialien/comma-list-iter.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100755 materialien/comma-list-iter.rs diff --git a/materialien/comma-list-iter.rs b/materialien/comma-list-iter.rs new file mode 100755 index 0000000..c599e27 --- /dev/null +++ b/materialien/comma-list-iter.rs @@ -0,0 +1,27 @@ +use std::str::FromStr; + +fn main() { + let res = parse_list("1, 2, 3; , ,7, 8,9; 6;3,5;"); + println!("{:#?}", res); + let res = parse_list("1, peter, 3, 4"); + println!("{:#?}", res); +} + +// "1, 2, 3; 7, 8,9; 6;3,5;" +fn parse_list(input: &str) + -> Result>, ::Err> +{ + // The following code could be expressed even shorter, but this is the + // exact code as shown in the lecture. + input.split(';') + .map(|s| s.trim()) + .filter(|s| !s.is_empty()) + .map(|s| { + s.split(',') + .map(|s| s.trim()) + .filter(|s| !s.is_empty()) + .map(|s| s.parse()) + .collect() + }) + .collect() +}