title: Unable to port C++ code that inserts into a trie to Rust due to multiple mutable borrows
body: <p>I have the following C++ code:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;vector&gt;
  #include &lt;string&gt;
  using namespace std;
  
  struct Trie {
    bool eow;         //end of word
    char val; 
    vector&lt;Trie&gt; chd; //children
  
    void push_word(const string&amp; word){
      Trie&amp; trie = *this;
      for (char c: word){
        if (trie.chd.empty() || trie.chd.back().val != c) {
          trie.chd.push_back(Trie{false, c, vector&lt;Trie&gt;{}});
        }
        trie = trie.chd.back();
      }
      trie.eow = true;
    }
  };
  </code></pre>
  
  <p>It's a trie for strings. <code>push_word</code> is supposed to only accept strings that are lexicographically greater than any word already contained in the trie; this way the search for the correct child can be skipped at each node. In other words, this allows us to efficiently construct a trie from a sorted vector of words:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>Trie from_sorted_vector(const vector&lt;string&gt;&amp; words){
    Trie trie{false, '\0', vector&lt;Trie&gt;{}};
    for (const auto&amp; word: words) {
      trie.push_word(word);
    }
    return trie;
  }
  </code></pre>
  
  <p>I have the following in Rust:</p>
  
  <pre><code>#[derive(Eq, PartialEq, Debug, Clone)]
  struct Trie {
      eow: bool,
      val: char,
      chd: Vec&lt;Trie&gt;,
  }
  
  impl Trie {
      fn new(eow: bool, val: char, chd: Vec&lt;Trie&gt;) -&gt; Trie {
          Trie {
              eow: eow,
              val: val,
              chd: chd,
          }
      }
  
      fn push_word(&amp;mut self, word: &amp;String) {
          let mut trie = self;
          for c in word.chars() {
              // ???
          }
      }
  }
  </code></pre>
  
  <p>I can't implement <code>push_word</code> in an analogous manner to C++. I always get two mutable borrows or one immutable and one mutable borrow, for <code>trie</code> or <code>trie.chd</code>, or the last element of <code>trie.chd</code>. I'd like to get some directions as to how this should be done.</p>