From 5a5a77e88fd78e1a301f30f295e8fab7912ce12a Mon Sep 17 00:00:00 2001 From: Ryan Geary Date: Wed, 11 Sep 2019 11:52:14 -0400 Subject: Unify enumerating lines in print_choice --- src/main.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 68ff3cb..60d769e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,21 +16,23 @@ enum Choice { impl Choice { fn print_choice(&self, line: &String, opt: &Opt) { - let words: Vec<&str> = line.split_whitespace().collect(); + let words = line.split_whitespace().into_iter().enumerate(); match self { Choice::Field(i) => { - if *i < words.len().try_into().unwrap() { - print!("{} ", words[*i as usize]); - } - }, + print!( + "{} ", + words + .filter(|x| x.0 == *i as usize) + .map(|x| x.1) + .collect::() + ); + } Choice::FieldRange(r) => match r { - (None, None) => print!("{}", words.into_iter().collect::()), + (None, None) => print!("{}", words.map(|x| x.1).collect::()), (Some(start), None) => print!( "{} ", words - .into_iter() - .enumerate() .filter(|x| x.0 >= (*start).try_into().unwrap()) .map(|x| x.1) .collect::>() @@ -45,8 +47,6 @@ impl Choice { print!( "{} ", words - .into_iter() - .enumerate() .filter(|x| x.0 < e) .map(|x| x.1) .collect::>() @@ -62,8 +62,6 @@ impl Choice { print!( "{} ", words - .into_iter() - .enumerate() .filter(|x| x.0 < e && x.0 >= (*start).try_into().unwrap()) .map(|x| x.1) .collect::>() -- cgit v1.2.3