---
title: "Comment by Raiyan Hossain on \"Build a Compiler from Scratch, Part 1.1: A Hello World of sorts\" | Hashnode"
description: "impl Iterator for Lexer&lt;'_&gt; { type Item = (Token, Span); fn next(&amp;mut self) -&gt; Option&lt;Self::Item&gt; { - self.next_token(); + self.emit_next_tok"
url: "https://hashnode.com/posts/build-a-compiler-from-scratch-part-11-a-hello-world-of-sorts/685b368a2c1dbdb423ba0c92/comment/688ee4dc3598c01e878a1013"
---

[Discussion](https://hashnode.com/posts/build-a-compiler-from-scratch-part-11-a-hello-world-of-sorts/685b368a2c1dbdb423ba0c92)

Comment [Build a Compiler from Scratch, Part 1.1: A Hello World of sorts](https://hashnode.com/posts/build-a-compiler-from-scratch-part-11-a-hello-world-of-sorts/685b368a2c1dbdb423ba0c92)

[Raiyan Hossain](https://hashnode.com/@zireael9797)

[Aug 3, 2025](https://hashnode.com/posts/build-a-compiler-from-scratch-part-11-a-hello-world-of-sorts/685b368a2c1dbdb423ba0c92/comment/688ee4dc3598c01e878a1013)

```
impl Iterator for Lexer<'_> {
    type Item = (Token, Span);

    fn next(&mut self) -> Option<Self::Item> {
-       self.next_token();
+       self.emit_next_tokens();
+       self.token_queue.pop_front()
    }
}
```

One question here

emit\_next\_tokens(); can push one or more tokens to the queue.

pop\_front(); only consumes one. Does this not mean the queue will race ahead of the iteration?

I tried setting type Item = (Token, Span, u32 /_Current Queue Size_/) with a longer bit of code with a lot of indentation and it did race ahead. It's possible I missed something else elsewhere.

[Geoffrey Copin](https://hashnode.com/@Sylver)

[Aug 3, 2025](https://hashnode.com/posts/build-a-compiler-from-scratch-part-11-a-hello-world-of-sorts/685b368a2c1dbdb423ba0c92/comment/688ee4dc3598c01e878a1013#reply-688fbdd33254f7291d881958)

You are absolutely correct! It would be better to check the length of the token queue before trying to emit new tokens. I edited the code accordingly
