mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-21 19:13:07 +00:00
Adds search items using given predicate in parallel example
cc # 330
This commit is contained in:
parent
b8aab7930c
commit
de94b25d40
2 changed files with 39 additions and 0 deletions
|
@ -3,6 +3,7 @@
|
|||
| Recipe | Crates | Categories |
|
||||
|--------|--------|------------|
|
||||
| [Mutate the elements of an array in parallel][ex-rayon-iter-mut] | [![rayon-badge]][rayon] | [![cat-concurrency-badge]][cat-concurrency] |
|
||||
| [Search items using given predicate in parallel][ex-rayon-parallel-search] | [![rayon-badge]][rayon] | [![cat-concurrency-badge]][cat-concurrency] |
|
||||
| [Sort a vector in parallel][ex-rayon-parallel-sort] | [![rayon-badge]][rayon] [![rand-badge]][rand] | [![cat-concurrency-badge]][cat-concurrency] |
|
||||
| [Generate jpg thumbnails in parallel][ex-rayon-thumbnails] | [![rayon-badge]][rayon] [![glob-badge]][glob] [![image-badge]][image] | [![cat-concurrency-badge]][cat-concurrency][![cat-filesystem-badge]][cat-filesystem] |
|
||||
| [Spawn a short-lived thread][ex-crossbeam-spawn] | [![crossbeam-badge]][crossbeam] | [![cat-concurrency-badge]][cat-concurrency] |
|
||||
|
@ -72,6 +73,40 @@ fn main() {
|
|||
}
|
||||
```
|
||||
|
||||
[ex-rayon-parallel-search]: #ex-rayon-parallel-search
|
||||
<a name="ex-rayon-parallel-search"></a>
|
||||
## Search items using given predicate in parallel
|
||||
|
||||
[![rayon-badge]][rayon] [![cat-concurrency-badge]][cat-concurrency]
|
||||
|
||||
This example uses [`rayon::find_any`] and [`par_iter`] to search a vector in
|
||||
parallel for an element satisfying the predicate in the given closure.
|
||||
|
||||
If there are multiple elements satisfying the predicate defined in the closure
|
||||
argument of [`rayon::find_any`], we are only guaranteed that one of them will be
|
||||
found, but not necessarily that the first will be found.
|
||||
|
||||
Also note that the argument to the closure is a reference to a reference
|
||||
(`&&x`). Please see the discussion on [`std::find`] for additional details.
|
||||
|
||||
```rust
|
||||
extern crate rayon;
|
||||
|
||||
use rayon::prelude::*;
|
||||
|
||||
fn main() {
|
||||
let v = vec![6, 2, 1, 9, 3, 8, 11];
|
||||
|
||||
let f1 = v.par_iter().find_any(|&&x| x == 9);
|
||||
let f2 = v.par_iter().find_any(|&&x| x % 2 == 0 && x > 6);
|
||||
let f3 = v.par_iter().find_any(|&&x| x > 8);
|
||||
|
||||
assert_eq!(f1, Some(&9));
|
||||
assert_eq!(f2, Some(&8));
|
||||
assert!(f3 > Some(&8));
|
||||
}
|
||||
```
|
||||
|
||||
[ex-rayon-thumbnails]: #ex-rayon-thumbnails
|
||||
<a name="ex-rayon-thumbnails"></a>
|
||||
## Generate jpg thumbnails in parallel
|
||||
|
@ -421,6 +456,8 @@ fn run() -> Result<()> {
|
|||
[`par_iter`]: https://docs.rs/rayon/*/rayon/iter/trait.IntoParallelRefIterator.html#tymethod.par_iter
|
||||
[`par_iter_mut`]: https://docs.rs/rayon/*/rayon/iter/trait.IntoParallelRefMutIterator.html#tymethod.par_iter_mut
|
||||
[`par_sort_unstable`]: https://docs.rs/rayon/*/rayon/slice/trait.ParallelSliceMut.html#method.par_sort_unstable
|
||||
[`rayon::find_any`]: https://docs.rs/rayon/*/rayon/iter/trait.ParallelIterator.html#method.find_any
|
||||
[`std::find`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.find
|
||||
[`Walkdir::new`]: https://docs.rs/walkdir/1.0.7/walkdir/struct.WalkDir.html#method.new
|
||||
|
||||
<!-- Other Reference -->
|
||||
|
|
|
@ -66,6 +66,7 @@ community. It needs and welcomes help. For details see
|
|||
| Recipe | Crates | Categories |
|
||||
|--------|--------|------------|
|
||||
| [Mutate the elements of an array in parallel][ex-rayon-iter-mut] | [![rayon-badge]][rayon] | [![cat-concurrency-badge]][cat-concurrency] |
|
||||
| [Search items using given predicate in parallel][ex-rayon-parallel-search] | [![rayon-badge]][rayon] | [![cat-concurrency-badge]][cat-concurrency] |
|
||||
| [Sort a vector in parallel][ex-rayon-parallel-sort] | [![rayon-badge]][rayon] [![rand-badge]][rand] | [![cat-concurrency-badge]][cat-concurrency] |
|
||||
| [Generate jpg thumbnails in parallel][ex-rayon-thumbnails] | [![rayon-badge]][rayon] [![glob-badge]][glob] [![image-badge]][image] | [![cat-concurrency-badge]][cat-concurrency][![cat-filesystem-badge]][cat-filesystem] |
|
||||
| [Spawn a short-lived thread][ex-crossbeam-spawn] | [![crossbeam-badge]][crossbeam] | [![cat-concurrency-badge]][cat-concurrency] |
|
||||
|
@ -198,6 +199,7 @@ community. It needs and welcomes help. For details see
|
|||
[ex-rand-range]: basics.html#ex-rand-range
|
||||
[ex-random-port-tcp]: net.html#ex-random-port-tcp
|
||||
[ex-rayon-iter-mut]: concurrency.html#ex-rayon-iter-mut
|
||||
[ex-rayon-parallel-search]: concurrency.html#ex-rayon-parallel-search
|
||||
[ex-rayon-parallel-sort]: concurrency.html#ex-rayon-parallel-sort
|
||||
[ex-rayon-thumbnails]: concurrency.html#ex-rayon-thumbnails
|
||||
[ex-regex-filter-log]: basics.html#ex-regex-filter-log
|
||||
|
|
Loading…
Reference in a new issue