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

Yeah, it's annoying that one can't return them unboxed to be easily able to construct zero-overhead lazily-evaluated sequences, but that's entirely unrelated to their avoid-bounds-checks property.

Instead of iterating over a range of indices and indexing, use the slice iterator, it's basically a drop-in replacement. The former can either be done with a manual loop & addition (which is not at all lazy and definitely can't be returned, i.e. Rust's current iterators are strictly more flexible), or it is done with the Range iterator. The two lazy forms are basically the same:

  let v: &[T] = ...;

  let x: iter::Map<ops::Range<usize>, _> = (0..n).map(|i| {
      let elem = &v[i];
      // do stuff
  })

  let y: iter::Map<slice::Iter<T>, _> = v.iter().map(|elem| {
      // do stuff
  });


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

Search: