Hacker News new | past | comments | ask | show | jobs | submit login

Now I’ve become confused as to what it means for a language to have a thing.

Hypothetically if you do

for i in 1..10 {

      println!("{i}");

  }
(From earlier) the compiler should have enough information to fully unroll the loop if it feels like it. So it is a different syntax from the programmer’s point of view. Maybe it goes through some “this is a loop” stage inside the compiler. But then hypothetically the assembly output could… end up not even having a loop at all (fully unrolled, no jump back, haha).



What your parent is saying is that the compiler treats that loop as

  {
      let result = match IntoIterator::into_iter(1..10) {
          mut iter => loop {
              let next;
              match iter.next() {
                  Some(val) => next = val,
                  None => break,
              };
              let i = next;
              let () = { println!("{i}"); };
          },
      };
      result
  }
(95% sure I got that correct)

And so in some sense, "loop" is more primitive than "for".

> But then hypothetically the assembly output could… end up not even having a loop at all

Absolutely, with any of these loops, the compiler may not emit a loop in assembly. And in this case, that seems to be true: https://godbolt.org/z/7Gq69Gvre




Join us for AI Startup School this June 16-17 in San Francisco!

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

Search: