frontpage.
newsnewestaskshowjobs

Open Source @Github

fp.

Open in hackernews

What `for x in y` hides from you – From Scratch Code

https://fromscratchcode.com/blog/what-for-x-in-y-hides-from-you/
12•rbanffy•1h ago

Comments

WhyNotHugo•1h ago
'for' loops in Rust do the same: they create an iterator and then iterate over that.

You can write the exact same loop with `let mut iter = v.iter(); while Some(x) = iter.next()`.

'for' loops in Rust are purely syntax sugar, and I somewhat wish they didn't exist. They provide you two ways of doing the same thing, but one of them hides the details from you. Having 'for' as a keyword is nice for folks coming from other languages, but then it hides the possibility of other interesting usages, like cloning an iterator inside a loop.

bigfishrunning•1h ago
Which is funny, because whenever I encounter a language for which `for` *doesn't* work this way it feels antiquated. I do however wish another keyword was used in many cases, becuase `for` in C and Go is so much different then `for` in Rust or Python. I think the higher-level case (Rust and Python) should really use a word like `foreach` or maybe something compeletely different like `itr`, although I get that they want to "look" more like C
rbanffy•28m ago
I would say Python really wants to look as much not-C as it can. In this case it's not the word "for" but the "for x in" construct.
Martinussen•1h ago
Is "hiding" in the sense that you just need to have read the docs or know how the language works at a pretty basic level really a problem, or even a negative? I would certainly say that the readability and clarity on the form of loop being used is a bigger win, either way.
tialaramex•58m ago
It's true that they're just sugar but Rust does explain how the for-in loop de-sugars and you've over-simplified considerably. Your syntax also doesn't quite work.

The value is in idiom, turning everything into loop expressions (The "while" keyword is also just sugar, Rust's only fundamental loop is named loop) makes it harder to discern what's actually going on.

If you want to clone the iterator in some cases rather than consuming it, that should look different so that reviewers will see what you're up to.

kevinmgranger•34m ago
It should be `v.into_iter()`, and that distinction matters because of ownership / move semantics.

It just so happens that for most collections, `IntoIter` is also defined for references to them, which typically gives you the same behavior that `.iter()` would give.

tialaramex•2m ago
More specifically for x in y consumes y in Rust.

https://doc.rust-lang.org/std/keyword.for.html explains how a loop is de-sugared

https://rust.godbolt.org/z/5jzhxYM51

... shows that today ranges like 0..5 aren't Copy even if that would be possible, which means if they're consumed they're gone, whereas an array of integers is Copy and so consuming it doesn't mean it's gone, you can just consume it again.

The desire is that Rust 2027 edition will change the nice syntax for ranges to produce new ranges like core::ranges::Range which are Copy if possible and only IntoIterator, the original ranges are never Copy but are Iterator, we now regret this choice.

saghm•22m ago
In addition to the points that sibling comments have made about how you're not quite right with the exact semantics of for loops (which also take ownership via `into_iter` and therefore are a bit different from `while let`), it's worth pointing out that if you peek a bit further in to MIR, all loops in Rust just desugar into the `loop` keyword with manual breaking, including `while`. It's not really clear to me why for loops in particular bother you.
anthonj•45m ago
I don't really get the point of the article. Even if I knew little about python, would be it surpsing that a language with no real basic types is probably abstracting a lot?

Even a simple i=0, i=i+1 is "hiding" a lot in python then.

PaulDavisThe1st•30m ago
But it's not a Python thing. Rust is noted below, and there's also C++.

  std::container<T> container;

  for (std::container<T>::iterator i = container.begin(); i != container.end(); ++i) { ...} 

  auto iter = container.begin(); while (iter != container.end()) { ...; ++iter; }

  for (auto const & t : container) { ... }
rbanffy•26m ago
Very few people who use Python realize the loop is not just looking into the values but asking the values to produce an iterator. It's only when they outgrow this early stage that they are ready to understand how to make a finite iterator themselves.
courtcircuits•26m ago
To be fair, when I started learning CS, the `for x in y` syntax was cryptic to me because I was unfamiliar with concepts such as iterators & generators. `for(int i=0; i<len(y); i++)` made way more sense since there is no hidden logic (besides additions as you highlighted in your comment, but which I think is easier to have a grasp of). So I really wish I had read this article when I started my CS journey a couple of years ago.
bux93•23m ago
In the author's mind, it's unexpected/amazing that 'for' can iterate over many types. But it's NOT unexpected/amazing that 'iter' can iterate over many types. I have no idea why.

It's not like 'for' is limited to counting in other languages. The grand-daddy in c does something until some condition is false, and that thing can equally be incrementing/decrementing a number or invoking some function. That's what a loop does in any case, it compiles down to a conditional jump (JNE/JE..)

Maybe his reason for astonishment is obscured by over-use of an LLM to 'enhance' the text.

mdemare•23m ago
AI slop. Flagged.
tantalor•2m ago
> Compared to i++ from C/C++ or forEach from JavaScript, Python's version just works.

Comparing to forEach in JS is incorrect because forEach is an method of Array.

You should compare it to `for...of` in JS. Both operate on iterators.

Article is missing an important distinction between iterators and other "array like" types (including strings):

Iterators don't have to stop, e.g., they can take from a generator that never ceases.

Both Python and JS are happy to loop forever if the iterator never stops.

thaumasiotes•7m ago
> It's not like 'for' is limited to counting in other languages. The grand-daddy in c does something until some condition is false

C 'for' is a while loop. It's strictly syntactic sugar for an already existing feature. And it's really, really transparent. `for(A; B; C) { do_stuff(); }` isn't just a while loop, it's this while loop:

    A;
    while(B) {
      do_stuff();
      C;
    }
Other languages have treated for as a separate concept from while. C isn't really informative in that case.

Sleep regularity is a stronger predictor of mortality risk than sleep duration (2023)

https://academic.oup.com/sleep/article/47/1/zsad253/7280269
332•bilsbie•3h ago•151 comments

Mysteries of Telegram Data Centers

https://dev.moe/en/3025
63•theanonymousone•1h ago•11 comments

Towards a Harness That Can Do Anything

https://eardatasci.github.io/c/ambiance/index.html
23•evakhoury•47m ago•3 comments

Briar Is in Maintenance Mode

https://briarproject.org/news/2026-maintenance-mode/
56•ristello•2h ago•19 comments

SpaceX bond worth 10% less than issue price – heading for junk bond status

https://www.ft.com/content/3a023b95-66c3-41e1-b0ce-df752a499541
219•youngtaff•1h ago•111 comments

Prioritize mental health, and why communication is so important

https://ramones.dev/posts/mental-health/
103•ramon156•3h ago•63 comments

Jurassic Park computers in excruciating detail

https://fabiensanglard.net/jurrasic_park_computers/index.html
702•vinhnx•11h ago•176 comments

The Three-Second Theft: Why AI Voice Fraud Outruns Every Defence

https://smarterarticles.co.uk/the-three-second-theft-why-ai-voice-fraud-outruns-every-defence
65•dxs•1h ago•61 comments

The well-calibrated Bayesian [pdf]

https://fitelson.org/seminar/dawid.pdf
12•Murfalo•44m ago•2 comments

Weathergotchi – an open-source climate Tamagotchi

https://github.com/Michael-Manning/E-Paper-Climate-Logger
57•luanmuniz•3h ago•16 comments

My Midlife Crisis Corolla Is Fast, Furious, and Modded

https://www.zocalopublicsquare.org/my-midlife-crisis-corolla-fast-furious-fully-modded/
4•gmays•32m ago•0 comments

Jiga (YC W21) is hiring the best people to make manufacturing great again

https://jiga.io/about-us/
1•grmmph•2h ago

Germany maybe found a new source of renewable energy

https://www.schweizerbart.de/papers/zdgg/detail/prepub/108503/Geological_and_geophysical_characte...
30•janandonly•2h ago•26 comments

US House of Representatives takes step to make daylight saving time permanent

https://www.bbc.com/news/articles/cz9l9venjd8o
12•throw0101d•1h ago•12 comments

A Trip to 90s Kansai: Exploring the XD FirstClass Network BBS

https://cdrom.ca/games/2026/05/30/xd.html
43•zetamax•1d ago•3 comments

Bootstrapping GDC with DMD

https://briancallahan.net/blog/20260713.html
12•LorenDB•1d ago•0 comments

Show HN: Grepathy – Claude made a decision nobody approved

https://github.com/evansjp/grepathy
13•evansjp•1h ago•17 comments

Vancouver PD website features Quick Escape button that wipes itself from history

https://vpd.ca/
323•LookAtThatBacon•14h ago•128 comments

What Every Python Developer Should Know About the CPython ABI

https://labs.quansight.org/blog/python-abi-abi3t
7•matt_d•3d ago•0 comments

What's the most popular number in Hacker News titles?

https://blog.omgmog.net/post/most-popular-numbers-in-hn-post-titles/
15•omgmog•2h ago•8 comments

Telegram Serverless

https://core.telegram.org/bots/serverless
84•soheilpro•4h ago•49 comments

TS-2026-009: Insecure argument handling in Tailscale SSH permitted root access

https://tailscale.com/security-bulletins
193•jervant•13h ago•120 comments

Show HN: I built a smart proxy so your coding agent can run loose

https://trollbridge.dev/
10•dandriscoll•23h ago•5 comments

Using Go for Mobile Apps

https://www.davidsobsessions.com/p/one-year-of-gomobile/
22•theHocineSaad•5h ago•8 comments

Pong Wars on the Commodore 64

https://imrannazar.com/articles/c64-pongwars
6•Two9A•1h ago•3 comments

What `for x in y` hides from you – From Scratch Code

https://fromscratchcode.com/blog/what-for-x-in-y-hides-from-you/
12•rbanffy•1h ago•16 comments

Latent Space as a New Medium

https://kevinkelly.substack.com/p/latent-space-as-a-new-medium
27•thm•1d ago•4 comments

Who's running all those tiny RPKI servers?

https://blog.apnic.net/2026/07/15/whos-running-all-those-tiny-rpki-servers/
62•enz•8h ago•12 comments

DSLs Enable Reliable Use of LLMs

https://martinfowler.com/articles/llm-and-dsls.html
85•SirOibaf•4h ago•56 comments

Microsoft Confirms Windows GDID Device Identifier That Cannot Be Disabled

https://www.ghacks.net/2026/07/12/microsoft-confirms-windows-gdid-device-identifier-that-cannot-b...
22•robtherobber•1h ago•5 comments