Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

There are tricks/an art to Rust's iterator semantics.

One example I know of is that when trying to calculate a dot product, you can improve the generated code and elide the bounds check only by a very particular syntax:

    // Must slice to equal lengths, and then bounds checks are eliminated!
    let len = cmp::min(xs.len(), ys.len());
    let mut xs = &xs[..len];
    let mut ys = &ys[..len];
    let mut s = 0.;
    for i in 0..xs.len() {
        s += xs[i] * ys[i];
    }
https://users.rust-lang.org/t/how-to-zip-two-slices-efficien...

https://github.com/rustsim/nalgebra/blob/b1b18d17ee64d3fd28b...



Does it still elide the bounds check if you replace the first three lines with

    assert_eq!(xs.len(), ys.len());

?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: