count_where/lib.rs
1
2 //! count_where adds a method to the Iterator trait that allows one to count the numbers of items matching a given predicate, much like a custom function in Swift that filters a sequence and return the count.
3 //! # Quick Start
4 //!
5 //! The easiest way to get things working, after adding the crate as a dependency would be like this:
6 //!
7 //! ```
8 //! use count_where::CountWhere;
9 //!
10 //! fn main() {
11 //! let numbers = [5, 5, 5, 2, 1];
12 //! let number = 5;
13 //! println!("{} appears {} times.", number, numbers.iter().count_where(|n| **n == number));
14 //! }
15 //! ```
16 //!
17 //! The above goes through a specified array and looks for each instance that the number is 5 and displays that to the user.
18
19mod count_where_ext; // make extension trait visible to library
20
21// give trait extension an easy to remember name
22pub use crate::count_where_ext::CountWhereExt as CountWhere;
23
24#[cfg(test)]
25mod tests {
26
27 use super::*;
28 #[test]
29 fn has_three_fives() {
30 let numbers = [5, 5, 5, 2, 1];
31
32 assert_eq!(3, numbers.iter().count_where(|n| **n == 5))
33 }
34
35 #[test]
36 fn has_two_numbers_less_than_five() {
37 let numbers = [5, 5, 5, 2, 1];
38
39 assert_eq!(2, numbers.iter().count_where(|n| **n < 5))
40 }
41}