4 releases

0.1.3 Jul 28, 2019
0.1.2 Jul 28, 2019
0.1.1 Jul 28, 2019
0.1.0 Jul 27, 2019

#31 in #per

MIT license

6KB
99 lines

Unchained

Trait that allows iterating on a collection while appliyng a function with a thread per item

Example

use unchained::Unchained;

// download all the pages at the same time by using a thread per item
fn download_all {
    let pages = vec![
        "https://doc.rust-lang.org/stable/std/",
        "https://doc.rust-lang.org/stable/std/#modules",
        "https://doc.rust-lang.org/stable/std/#primitives",
        "https://doc.rust-lang.org/stable/std/#macros",
        "https://doc.rust-lang.org/stable/std/prelude/index.html",
        "https://doc.rust-lang.org/stable/book/ch03-02-data-types.html",
    ];
    download_all_inner(pages);
}

fn download_all_inner(pages: Vec<&'static str>) {
    pages
        .into_iter()
        .unchained_for_each(|page| {
            println!(
                "{}:\n\n{}",
                &page,
                String::from_utf8(
                    std::process::Command::new("curl")
                        .arg(&page)
                        .output()
                        .unwrap()
                        .stdout
                )
                .unwrap()
            );
            println!("\n#######################\n");
        });
}

You can also checkout rustman that uses Unchained to parallelize search requests

No runtime deps