#indices #slice #split #rayon

select_indices

Iterators for taking multiple shared/exclusive references from a slice

4 releases (stable)

3.0.1-alpha-2 Feb 9, 2022
3.0.1-alpha Feb 8, 2022
3.0.0 Mar 21, 2022
3.0.0-alpha Dec 6, 2021
0.2.2 Nov 14, 2021

#2085 in Rust patterns

25 downloads per month

MPL-2.0 license

55KB
1K SLoC

select_indices

GitHub Workflow Status crates.io docs.rs Crates.io

select_indices is a crate that provides iterators for seeking through a slice with a pre-made list of indices. It can simplify the readability of code and, in some cases, increase performance.

Documentation

use select_indices::prelude::*;

fn main()
{
    struct BankAccount {
        pub name: String,
        pub balance: f32,
    }
    
    let mut vec: Vec<BankAccount> = vec![
        BankAccount { name: "Joey Bag o' Donuts".to_string(), balance: 4.27 },
        BankAccount { name: "Henry Howard Roosevelt".to_string(), balance: 83.20 },
        BankAccount { name: "Jenny Jenson".to_string(), balance: 54.32 },
        BankAccount { name: "The Dude".to_string(), balance: -134.01 },
        // Assume there's like 300 of these
    ];
    
    vec.select_indices_mut(&[1, 3]).for_each(|account| {
        account.balance -= 20.00;
        println!("{} now has ${}", account.name, account.balance);
    });
}

There is also a rayon feature flag that provides ParallelIterator versions of select_indices iterators. In certain cases, these iterators can greatly improve performance over other methods of slice iteration.

Dependencies

~5–560KB
~11K SLoC