Crates.io | has-some |
lib.rs | has-some |
version | |
source | src |
created_at | 2024-12-06 18:12:51.716284 |
updated_at | 2024-12-07 13:52:50.26816 |
description | The opposite of is_empty (and is_empty for filters) |
homepage | https://github.com/bassmanitram/has-some |
repository | https://github.com/bassmanitram/has-some |
max_upload_size | |
id | 1474503 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
Implement the opposite of is_empty
to promote better semantics for iterator
filters (and other situations) where !T.is_empty()
is counterintuitive, as
well as introduce filter_friendly versions of T::is_empty
.
Using is_empty
in an iterator filter
method is relatively verbose because
you can't pass the T::is_empty
function when the iterator item is a reference,
and, anyway, you usually want to retain things that are not empty, a predicate
for which you'll always need a closure.
Basically, it stands that the semantics of "not is_empty" are annoying (well, to me)
when "has some" is clearer, and even T::is_empty
is annoying when using filters.
This crate, then, addresses those annoyances.
It's not really rocket science, but here you go with an example where is_empty
passed
as a function reference to an iterator filter does work:
use has_some::HasSome
let vector = vec!["some_data".to_owned(), "".to_owned(), "more data".to_owned(), "".to_owned()];
let vector2 = vector.clone();
// If you want the empties, you can do
let empties = vector.into_iter().filter(String::is_empty).collect::<Vec<String>>();
assert_eq!(["", ""], empties.as_slice());
// If you want the non-empties, you can now do
let non_empties = vector2.into_iter().filter(String::has_some).collect::<Vec<String>>();
assert_eq!(["some_data", "more data"], non_empties.as_slice());
And a common example where you have Items
that are double references:
use has_some::HasSome
let vector = vec!["some_data", "", "more data", ""];
// If you want the empties, you can do
let empties = vector.iter().filter(str::is_empty3).collect::<Vec<&&str>>();
assert_eq!([&"", &""], empties.as_slice());
// If you want the non-empties, you can now do
let non_empties = vector2.iter().filter(str::has_some3).collect::<Vec<&&str>>();
assert_eq!([&"some_data", &"more data"], non_empties.as_slice());
str
Just has_some