‹ BackHN Continuity

Thread

What Zig felt like, coming from Rust

282 points · 351 comments · ksec

  1. sgarland · · focus · HN ↗
    From TFA, on combinators vs. loops:

        items.iter().enumerate().filter(|(_, i)| cond(i)).map(|(idx, i)| Pointer::idx(i, path, idx)).collect()
    
        while (i < cursors.len) {
            if (actual_index < arr.items.len) { cursors[i] = .{...}; i += 1; }
            else iteration.remove(i);
        }
    
    I’ve been slowly learning Rust, and this style is my main gripe against it, because I feel like I’m being gaslit. Its proponents praise its readability and ease of use, and just… no. It looks deranged. A simple loop is immediately obvious to anyone who’s programmed in any language. Even Python’s list comprehensions are loop-ish.
    1. himata4113 · · focus · HN ↗

        items.filter(|(i)| cond(i)).map(Pointer::idx).collect()
      
      you can probably get away with this if you implement a Trait, not sure how but I know for a fact this is possible, idk why there's an enumerate there when you aren't even using it.

      items is already an iteratible so you can do direct .filter on it as well

      tl;dr if the code looks ugly you're probably not taking advantage of a language feature that allows it to look pretty.

      1. sgarland · · focus · HN ↗
        This is in fact more readable, thank you.

        I still don’t know that I find it more intuitive or readable than the simple loop, but it’s much less awful than before.

        1. himata4113 · · focus · HN ↗
          I am sure there's a way to filter directly on cond variable and collect isn't needed most of the time since iterators are way more useful in general (unless you want to print that data)

             items.filter(cond).map(Pointer::idx)
          
          I know this is possible, but you would have to consult some rust wizard for this.
Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.