| Crates.io | pluck |
| lib.rs | pluck |
| version | 0.1.1 |
| created_at | 2020-08-29 20:42:06.639168+00 |
| updated_at | 2020-08-29 20:54:21.212252+00 |
| description | Extract values conveniently. |
| homepage | https://github.com/shelbyd/pluck |
| repository | https://github.com/shelbyd/pluck |
| max_upload_size | |
| id | 282441 |
| size | 10,679 |
pluck! is a macro that creates a lambda that plucks the provided
property from the argument. Great with iterators.
pluck! provides many different ways to access values.
Provide the index to extract.
let list = [(0, "a"), (1, "b"), (2, "c")];
let first = list.iter().map(pluck!(.0)).collect::<Vec<_>>();
assert_eq!(first, &[0, 1, 2]);
let second = list.iter().map(pluck!(.1)).collect::<Vec<_>>();
assert_eq!(second, &["a", "b", "c"]);
Provide the property name to extract.
struct Person { name: &'static str }
let list = [Person { name: "Alice" }];
let names = list.iter().map(pluck!(.name)).collect::<Vec<_>>();
assert_eq!(names, &["Alice"]);
Precede the property name with & to pluck by reference.
let list = [(0, "a"), (1, "b"), (2, "c")];
let first = list.iter().map(pluck!(&.0)).collect::<Vec<_>>();
assert_eq!(first, &[&0, &1, &2]);
Precede the property name with &mut to pluck by mutable reference.
let mut list = [(0, "a"), (1, "b"), (2, "c")];
for num in list.iter_mut().map(pluck!(&mut .0)) {
*num += 1;
}
assert_eq!(list, [(1, "a"), (2, "b"), (3, "c")]);
pluck! works with types implementing Index and
IndexMut.
let list = [[0], [1], [2]];
let first = list.iter().map(pluck!([0])).collect::<Vec<_>>();
assert_eq!(first, &[0, 1, 2]);
let first_ref = list.iter().map(pluck!(&[0])).collect::<Vec<_>>();
assert_eq!(first_ref, &[&0, &1, &2]);
pluck! works with types implementing Deref and
DerefMut.
let list = vec![&0, &1, &2];
let derefed = list.into_iter().map(pluck!(*)).collect::<Vec<_>>();
assert_eq!(derefed, &[0, 1, 2]);
let list = vec![&&&0, &&&1, &&&2];
let derefed = list.into_iter().map(pluck!(***)).collect::<Vec<_>>();
assert_eq!(derefed, &[0, 1, 2]);
pluck! is designed to allow you to arbitrarily combine accessing. You
can specify precedence with ().
struct Person { name: &'static str }
let mut list = vec![[&Person { name: "Alice" }]];
let derefed = list.iter_mut().map(pluck!((*[0]).name)).collect::<Vec<_>>();
assert_eq!(derefed, &["Alice"]);