title: How to modify/partially remove a range from a BTreeMap?
body: <p>I'm trying to build a <code>RangeSet</code> out of a <code>BTreeMap</code> (where the keys are lower bounds and the values are upper bounds). This works quite well as long as I'm only looking up stuff. However, the first mutating method has me stumped:</p>
  
  <p>If I want to <em>insert</em> a range to my set, I need to check a <code>Range</code> of my <code>BTreeMap</code> from the upper bound of the range from tail to head until the value in question is below the inserted range's lower bound, all contained ranges should be <em>removed</em> and overlapping ranges <em>merged</em>. However, even using <code>.range_mut(..)</code> which is currently unstable, I cannot find a way to <em>delete</em> an entry (because the map is still borrowed from by the range).</p>
  
  <p>Am I using the wrong tool for the job? Can I make it work anyway – and if so, how? My current solution in pseudocode (I also use my own <code>Cut</code> type because <code>Bound</code>s are not <code>Ord</code>):</p>
  
  <pre><code>fn insert(&amp;mut self, range: &amp;Range) {
      for entry in self.map.range_mut(Cut::Unbounded, range.upper).rev() {
           if entry.1 &lt; range.lower {
               break;
           } else if entry.0 &gt; range.lower {
               delete entry
           } else {
               merge entry with range
           }
       }
  }
  </code></pre>