News for hackers. Stuff that matters.
Re:Zig's Curious Multi-Sequence for Loops by masklinn on Monday February 27, 2023 @11:43AM (#34958584)
That's nothing special though, `zip` just takes an item from each iterator, packs them into a tuple, and yields that. It has no weird bounds or requirements or anything: https://doc.rust-lang.org/std/iter/fn.zip.html

The impl of the default `next` is:

    fn next(&mut self) -> Option<(A::Item, B::Item)> {
        let x = self.a.next()?;
        let y = self.b.next()?;
        Some((x, y))
    }
So completely straightforward.
Re:Zig's Curious Multi-Sequence for Loops by defen on Monday February 27, 2023 @12:35PM (#34959362)
Can that zip more than two iterators? And does it perform a bounds check on each call to `a.next()` and `b.next()`?
Re:Zig's Curious Multi-Sequence for Loops by brundolf on Monday February 27, 2023 @12:38PM (#34959395)
It stops when one of the two iterators ends

It can't zip more than two per se, but you could zip the result of the first zip into a third and get ((item1, item2), item3). You could then map these if you wanted, to flatten them into a single tuple .map(|((item1, item2), item3)| (item1, item2, item3))

Of course there's a trade-off here between ergonomics and generality

Re:Zig's Curious Multi-Sequence for Loops by masklinn on Monday February 27, 2023 @12:43PM (#34959476)
> It can't zip more than two per se, but you could zip the result of the first zip into a third and get ((item1, item2), item3). You could then map these if you wanted, to flatten them into a single tuple .map(|((item1, item2), item3)| (item1, item2, item3))

FWIW that's more or less what `itertools::izip!` does for you, it just chains `zip`s then "splats" them using a `map`.

Re:Zig's Curious Multi-Sequence for Loops by defen on Monday February 27, 2023 @12:44PM (#34959493)
> It stops when one of the two iterators ends

Right; my question is, suppose you're iterating over two slice iterators - won't each call to `a.next()` and `b.next()` have to check whether that sub-iterator is done? One of the benefits of the Zig approach is that you can iterate over an arbitrary number of slices and do one check before entering the loop, followed by the compiler emitting unchecked index access in the loop. So it basically compiles down to the equivalent of a C `for` loop.

Re:Zig's Curious Multi-Sequence for Loops by masklinn on Monday February 27, 2023 @12:58PM (#34959682)
Rust's zip has a specialisation for iterators with a trusted length. Such as slice iterators.

`zip` yields exactly the same assembly as a loop over the index range with an unsafe item access: https://godbolt.org/z/7ebfxbhxc

Re:Zig's Curious Multi-Sequence for Loops by brundolf on Monday February 27, 2023 @01:32PM (#34960135)
Interesting, are "trusted-length" iterators something that might ever make it into userspace? Maybe as const generics?
Re:Zig's Curious Multi-Sequence for Loops by masklinn on Monday February 27, 2023 @01:39PM (#34960222)
It’s already in userspace, though nightly (and unsafe, obviously), so weather it’ll be stabilised, and in what form, is an open question: https://doc.rust-lang.org/std/iter/trait.TrustedLen.html
Re:Zig's Curious Multi-Sequence for Loops by kristoff_it on Monday February 27, 2023 @03:10PM (#34961256)
You have to pass -O though, the point of Zig's for loop syntax is to get fast compile times and good performance also in debug mode :^)
Re:Zig's Curious Multi-Sequence for Loops by defen on Monday February 27, 2023 @03:22PM (#34961389)
That's cool. At the same time though, it almost feels like a distinction without a difference in some ways - Zig has a special built-in syntax; Rust doesn't use special syntax, but it does use complex special-cased unsafe code in the stdlib in order to implement a safe + performant API.
Re:Zig's Curious Multi-Sequence for Loops by masklinn on Monday February 27, 2023 @03:29PM (#34961472)
On the other hand, the "special cased unsafe code" is applicable to more than just zip, more than just the one array type, and is available in userland (though currently unstable so nightly only, both to implement it on a bespoke type and to rely on it).
Re:Zig's Curious Multi-Sequence for Loops by brundolf on Monday February 27, 2023 @03:44PM (#34961605)
Rust's is built on top of (and exposed to) Iterators, which are a very general concept that can be rooted in all kinds of data structures, composed in all kinds of ways, and collected/processed in all kinds of ways (i.e. the user's code might not even contain an actual loop). The code continues to work in many situations, even where the optimization doesn't apply

You trade some special-case syntax and ergonomics for that generality, but it is very general even if not all of it is optimized in the same way

Re:Zig's Curious Multi-Sequence for Loops by the8472 on Monday February 27, 2023 @03:25PM (#34961426)
For Zip it's TrustedRandomAccess[0] instead of TrustedLen. Imo the most radioactively unsafe trait in the standard library and will likely never be stabilized in its current form.

[0] https://github.com/rust-lang/rust/blob/f540a25745e03cfe9eac7...

Re:Zig's Curious Multi-Sequence for Loops by chlorion on Monday February 27, 2023 @08:28PM (#34964555)
You can zip a zipper to combine them, but you end up with an item like (a, (b, (c, d))), not a huge deal since it gets destructured automatically.

The itertools crate has a multizip function/macro that allows zipping as many iterators as you'd like without nesting zips inside of zips!

I really recommend checking out itertools if you use rust and like iterators! It's a direct dependency of rustc itself too, which suggests its a trusted project (it may even be maintained by rust devs, not totally sure).

Re:Zig's Curious Multi-Sequence for Loops by brundolf on Monday February 27, 2023 @11:56AM (#34958789)
I was thinking about the fact that whatever you're iterating over has to be copied around throughout the process. Rust can guarantee that eg. deep-copies (clones) of allocated structs will never happen implicitly, if your iterator owns the values being iterated. But in languages where copying can trigger allocations, this could be a problem

I don't actually know whether that applies to Zig though

Re:Zig's Curious Multi-Sequence for Loops by laserbeam on Monday February 27, 2023 @11:00PM (#34965459)
You're assuming that an iterator gets created and returned by zip. In zig the for loop syntax doesn't need the concept of an iterator. All you need is variables in a capture.

That's what I mean by straightforward. You'd have to argue about the memory layout for an iterator, that you call functions on it etc. Lots of small decisions. In zig those things would usually be part of the standard library, not language features.

If you want to call a next() function on a thing in zig, you want to be explicit. That's what the language means by "no hidden control flow". You'd use a while loop if you wanted to iterate on a hashmap for example.

In some sense, this is less elegant, but it's usually more obvious what the compiler would end up doing in any bit of code.