From 19773a0959364ef7c54f14049c07b030c443cd1a Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Thu, 3 Oct 2019 12:38:24 +0300 Subject: Change: Roadmap.step_names to return an iterator --- src/lib.rs | 2 +- src/parser.rs | 2 +- src/roadmap.rs | 14 +++++--------- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c13943d..4eb712a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,7 @@ //! label: The first step //! ").unwrap(); //! -//! let n = r.step_names(); +//! let n: Vec<&str> = r.step_names().collect(); //! assert_eq!(n.len(), 2); //! assert!(n.contains(&"first")); //! assert!(n.contains(&"endgoal")); diff --git a/src/parser.rs b/src/parser.rs index f5eaf96..8b0dc05 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -176,7 +176,7 @@ second: ) .unwrap(); - let names = roadmap.step_names(); + let names: Vec<&str> = roadmap.step_names().collect(); assert_eq!(names.len(), 2); assert!(names.contains(&"first")); assert!(names.contains(&"second")); diff --git a/src/roadmap.rs b/src/roadmap.rs index 778aa2f..7b78572 100644 --- a/src/roadmap.rs +++ b/src/roadmap.rs @@ -25,12 +25,8 @@ impl Roadmap { } /// Return list of step names. - pub fn step_names<'a>(&'a self) -> Vec<&'a str> { - let mut names = vec![]; - for step in self.steps.iter() { - names.push(step.name()); - } - names + pub fn step_names<'a>(&'a self) -> impl Iterator { + self.steps.iter().map(|step| step.name()) } /// Get a step, given its name. @@ -190,7 +186,7 @@ mod tests { #[test] fn new_roadmap() { let roadmap = Roadmap::new(); - assert_eq!(roadmap.step_names().len(), 0); + assert_eq!(roadmap.step_names().count(), 0); } #[test] @@ -198,8 +194,8 @@ mod tests { let mut roadmap = Roadmap::new(); let first = Step::new("first", "the first step"); roadmap.add_step(&first).unwrap(); - assert_eq!(roadmap.step_names().len(), 1); - assert_eq!(roadmap.step_names(), vec!["first"]); + let names: Vec<&str> = roadmap.step_names().collect(); + assert_eq!(names, vec!["first"]); } #[test] -- cgit v1.2.1