title: Iterator with `&mut` items
body: <p>I want to create an iterator for iterating through records in a huge file.</p>
  
  <pre><code>for record in huge_file.iter() {
    println!("{}", record.read_field("name"));
  }
  </code></pre>
  
  <p>Each record individually is too big to fit in memory, so the iterator can't return owned <code>Record</code> objects with everything loaded into memory.  The underlying storage does not support random access, so returning "slices"/"views" of the storage is also not an option.</p>
  
  <p>Instead, I want the iterator to return elements that allow the consumer to read from the current "cursor position" in the file.  Superficially, the API would look like this:</p>
  
  <pre><code>impl Iterator for HugeFileIterator {
    fn next&lt;'a&gt;(&amp;'a mut self) -&gt; Option&lt;&amp;'a mut Record&gt; {
      ...
    }
  }
  
  impl Record {
    fn read_field(name: &amp;str) -&gt; &amp;str {
      ...
    }
  }
  </code></pre>
  
  <p>With that signature of <code>next</code>, the <code>mut</code> borrow ensures that the consumer can't keep a reference to the "previous <code>Record</code>" when calling <code>next()</code> to get the "next <code>Record</code>."</p>
  
  <p>However, I can't get that type signature for <code>next</code> to work the way I want it to.  The <code>Iterator</code> trait requires me to define an associated <code>Item</code> type, and I don't know how it should look:</p>
  
  <pre><code>impl Iterator for HugeFileIterator {
    type Item&lt;'a&gt; = &amp;'a mut Record; // ← Obviously doesn't work
  
    fn next&lt;'a&gt;(&amp;'a mut self) -&gt; Option&lt;Self::Item&lt;'a&gt;&gt; {
      ...
    }
  }
  </code></pre>
  
  <p>Is it possible to express this constraint in the Rust type system?  Or is there perhaps another completely different approach that I haven't thought about?</p>