Find Position Pairs Index With Item

Locate the first matching element with find_position, which returns the index and the item together in one pass on both borrowed and owning iterators.

RustData Integrity

Code

rustsnippet
15 lines
fn main() {
    let queue = ["build", "test", "sign", "upload"];

    // find_position 一次返回下标和元素:不需要先 position 再回取
    let hit = queue.iter().find_position(|task| **task == "sign");
    println!("hit: {hit:?}");

    let miss = queue.iter().find_position(|task| task.starts_with("deploy"));
    println!("miss: {miss:?}");

    // 消费所有权版本:into_iter 之上同样可用
    let owned: Vec<String> = vec!["lint".to_string(), "ship".to_string()];
    let found = owned.into_iter().find_position(|task| task == "ship");
    println!("owned: {found:?}");
}

Order of operations

Find Position Pairs Index With Item keeps everything in the function main.

Caveats

  • No external surface exists — the standard library supplies all behavior.

Using it

The one callable in play: fn main()

The printout labels itself — hit:, miss:, owned: — when the file runs.

Key statements

let miss = queue.iter().find_position(|task| task.starts_with("deploy"));
let hit = queue.iter().find_position(|task| **task == "sign");

Interactive Tools for This Topic

FAQ

What does Find Position Pairs Index With Item print when run?
Run it as a complete program — the output lines carry labels such as `hit:`, `miss:`, `owned:`.