Crates.io | splop |
lib.rs | splop |
version | 0.2.0 |
source | src |
created_at | 2018-06-29 18:21:29.42861 |
updated_at | 2018-06-30 17:02:11.421142 |
description | Helper functions to determine the first/last repetition of something. |
homepage | |
repository | https://github.com/LukasKalbertodt/splop |
max_upload_size | |
id | 72248 |
size | 30,595 |
This tiny crate contains functions and types to help you do something special when repeating for the first or last time (or in between!). This crate offers two distinct features:
IterStatusExt::with_status
: a new method for iterators, that
creates a new iterator which yields the item paired with information to
tell you if this is the first/last item.
SkipFirst
: a simple struct to help you always do something, except on
the first repetition. Works without iterators, too!
(also see examples/
)
let names = ["anna", "peter", "brigitte", "bob"];
for (name, status) in names.iter().with_status() {
print!("{}", name);
if status.is_first() {
print!(" <-- winner (ᵔᴥᵔ)");
}
if status.is_last_only() {
print!(" ... ʘ︵ʘ");
}
println!();
}
The old programming problem – commas in between elements:
let mut comma = SkipFirst::new();
print!("[");
for name in &["banana", "melon", "kiwi"] {
comma.skip_first(|| print!(", "));
print!("{}", name);
}
println!("]");
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.