| Crates.io | has-some |
| lib.rs | has-some |
| version | 2.0.1 |
| created_at | 2024-12-06 18:12:51.716284+00 |
| updated_at | 2024-12-20 08:40:58.74381+00 |
| 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 |
| size | 27,963 |
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());
strJust has_some