Optimizing away bounds checks when zipping two iters of the same length

⚓ Rust    📅 2025-12-13    👤 surdeus    👁️ 1      

surdeus

Consider Rust Playground

fn bla(a: Vec<i64>, b: Vec<i64>) -> i64 {
    let mut r = 0i64;
    assert!(a.len() == b.len());
    for (x,y) in a.iter().zip(b.iter()) {
        r += x+y;
    }
    r
}

Does rustc optimize away the bounds checks in the loops because it knows from the assert! that the two zipped vectors have the same length?

1 post - 1 participant

Read full topic

🏷️ Rust_feed