title: Lazy sequence generation in Rust
body: <p>How can I create what other languages call a lazy sequence or a "generator" function?</p>
  
  <p>In Python, I can use <code>yield</code> as in the following example (from Python's docs) to lazily generate a sequence that is iterable in a way that does not use the memory of an intermediary list:</p>
  
  <pre class="lang-py prettyprint-override"><code># a generator that yields items instead of returning a list
  def firstn(n):
      num = 0
      while num &lt; n:
          yield num
          num += 1
  
  sum_of_first_n = sum(firstn(1000000))
  </code></pre>
  
  <p>How can I do something similar in Rust?</p>