#rust

/

      • WindowsBunny has quit
      • stine
        yeah, that's a really good idea, thanks!
      • niconii joined the channel
      • rkruppe
        ideasman42: if you loop over indices you need to make sure you don't skip over elements when removing
      • pzol_ joined the channel
      • ideasman42
        sure, its up to you do manage how you step over values... but this is typical lower level code
      • TheCycoONE has quit
      • tscheck has quit
      • danpker has quit
      • yehnra joined the channel
      • TheCycoONE joined the channel
      • WindowsBunny joined the channel
      • stine
        i did consider that but eg for 0..v.len(), i don't know when v.len() is calculated, it would have to be on every step for it to not overflow, you could also do let mut n = v.len(), for i in 0..n { ... } and then decrement both n and i as you go, but that sounded a bit complicated
      • rkruppe
        I mostly mention that because it is reasonably common to forget that, and it's one thing iterators protect you from
      • ideasman42
        you would use `while index < vector.len()`
      • or, if you can loop backwards to zero
      • karinqe joined the channel
      • Quxxy2 has quit
      • its often faster removing from the end of the array
      • rkruppe
        still O(n^2) though
      • Ms2ger
        Huh?
      • stine
        ah, using while is a good idea
      • rkruppe
        retain is better on almost all accounts -- faster, less error prone, more self documenting
      • raichoo has quit
      • nox
        retain move stuff around.
      • stephaneyfx has quit
      • ideasman42
        issue is if this is part of a larger loop and your not _only_ removing indices
      • prz joined the channel
      • of course if entire loops purpose is to remove from vector - use retain
      • rkruppe
        yes, it's not always possible
      • gvanderest has quit
      • SiegeLord joined the channel
      • ideasman42
        rkruppe, only O(n^2) if you dont use swap-remove. Very nice rust has this feature :)
      • rkruppe
        but you should try hard to formulate your problem so that it works. splitting in multiple loops can sometimes be worthwhile too
      • ideasman42: yes but that messes up the order so it's not always possible either
      • prz_ joined the channel
      • prz has quit
      • ideasman42
        sure, if it makes sense