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

On the advent of code day three it would be useful to have a chunking iterator in rust. Having just implemented day 3 and looking for one, I knew rust stable doesn’t have a general purpose chunking iterator.

I asked chatGPT about it and it very confidently said one existed and gave me a sample code which imported a nonexistent chunking iterator. I wonder about other outputs of chatGPT which are not as easily and quickly verifiable.



When I asked it how to undo an operation I had performed using "cargo add" it confidently showed me examples of using the "cargo remove" command... which it had entirely made up.


ChatGPT is on nightly of course, https://doc.rust-lang.org/nightly/cargo/commands/cargo-remov...

It should have told you `cargo +nightly remove <dep>`


There is chunk operator on std::slice.

How would that work on general iterators? You can have an iterator that always returns 'y'. Or buffered iteration or whatever.


See for yourself:

https://doc.rust-lang.org/std/iter/trait.Iterator.html#metho...

As the parent said, it's not stable yet but it's right there so you can see how it works.

The most important ergonomic trick here is that Rust has type inference, it can see array_chunks needs to know how big the chunks should be, and of course you can just specify that but in most cases you'll use chunks which clearly have a defined size and the inference will go "Oh, that's how big the chunks are" just as e.g. if you put a bunch of chars in a Vec you made, the type inference goes, "Oh, it's a Vec of chars" and doesn't waste your time asking you what type of Vec it is.

You'll see the example expects Some(['l', 'o']) to be the first thing out of the Iterator, the type inference system can see that's an array of two chars, thus, the chunk size is 2.


Yeah, but does seem to function bit different than chunks. Plus it's unstable and as expected has perf issues.

It being nightly doesn't mean it will be stabilized.




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

Search: