count_where

Crates.iocount_where
lib.rscount_where
version0.1.1
sourcesrc
created_at2021-06-06 20:17:30.149628
updated_at2021-06-07 16:21:36.413022
descriptionlibrary that adds a count_where method to Iterator, to easily count items matching a condition
homepagehttps://github.com/bryceac/count_where
repositoryhttps://github.com/bryceac/count_where
max_upload_size
id407011
size7,958
(bryceac)

documentation

README

=count_where=

'''Author:''' Bryce Campbell

'''Description:''' A single trait library that adds an iterator method allowing one to easily count up elements matching a particular condition.

'''License:''' See LICENSE

==Version History==

  • 0.1.1

    Released June 7, 2021

    Allows comparison booleans in closure.

  • 0.1

    Released June 6, 2021

    Initial release.

==Notes==

This library has only been tested with Rust 1.51.0 on macOS Big Sur, but should work on any platform.

===Why Create this Library?===

I like dabbling in programming from time to time, and one day I tried to make a Rust equivalent of this project, to help me gain more experience with Rust.

While I could figure out most things, I had a bit of trouble with how I was implementing things, such as a counting method to determine how many times a number occurred.

Even though I could create a function that took the data and a number to look for, it seemed to be not quite as nice as the extension I wrote for the Swift variant, which worked for all Sequences that had items that conformed to Equatable.

As a result, I decided to look into creating an extension on Iterator in Rust, so that I could do something like this in Rust:

sequence.count { $0 == number }

Not only did I kind of succeed when writing the code for the library, but it gave me a good chance to see how to make a ttrait extension and creating a library right in Rust.

While this library does not provide the same simplicity as the Swift code seen above, it still provides the same convenience, which is what I needed.

===Usage===

To use this library, add the following to the Cargo.toml file:

[dependencies]
count_where = "0.1.0"

Afterwards, it is as simple as putting the following in the source file needing the method:

use count_where::CountWhere;

You can then use code like this to do your counting:

fn main() {
    let numbers = [5, 5, 5, 2, 1];

    let number = 5

    println!("{} appears {} times", number, numbers.iter().count_where(|n| **n == number));
}

Because n is of type &&T, it needs to be dereferenced as seen above, in order to make the comparison.

This could change in the future, which I would have liked to do, but because I am not as experienced with Rust as I am with Swift, this will be something I need to live with.

===Contributing===

If you would like to contribute to this library, feel free to create a fork and send your modifications.

One thing that I would really like is for the provided function to usable in a way lie the filter method, which is utilitized like this:

fn main() {
    let numbers = [5, 5, 5, 2, 1];
    let filtered_numbers = numbers.iter().filter(|n| n == 5);

    println!("{}", filtered_numbers);
}

===Support===

Due to my experience with Rust being limited, in comparison to Swift, I cannot provide that much support, so expect to be on your own.

However, I can try to check things out, so feel free contact me at the email below.

tonyhawk2100@gmail.com

Commit count: 22

cargo fmt