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

> I'm sorry, but I don't understand what you mean by bounds checks being done at the slice level and not per-element access. A statement like "pixels[y * stride + x] = etc" is per-element, right?

Yes, if you index a slice you have to check each access. However the idiomatic way to work with strides of data in Rust is to use Iterators.

Bounds is checked at the entry of an iteration and the the inner loop is nice and fast. So your example would be:

  for pixel in &mut pixels[0..y*stride+x] {
    *pixel = etc
  }
I tried to do something similar on the playground[1] but it turns out Rust/LLVM is too smart and folded the whole loop down to a constant.

[1] https://play.rust-lang.org/?gist=f3699d6456a561c3874395bff36...



https://rust.godbolt.org/ is good for playing around with this stuff: https://godbolt.org/g/9TgVXg


Sure, the inner loop is nice and fast if you're consuming exactly one byte per iteration, but I'm not doing that. In any case, I asked https://users.rust-lang.org/t/iterators-and-eliminating-all-...




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

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

Search: