title: Is there a way to join a BTreeSet on whitespace while consuming it to get a string without pushing each element to a string?
body: <p>Is there a way to join a <code>BTreeSet</code> on whitespace while consuming it to get a string with each element separated by whitespace, other than by iterating over each element and pushing to a string?</p>
  
  <p>My set can be very large, perhaps 10GB at most, so I would like to limit memory usage.</p>
  
  <pre><code>type `std::collections::BTreeSet&lt;std::vec::Vec&lt;u8&gt;&gt;`
  </code></pre>
  
  <p>I am using the <a href="https://crates.io/crates/bio" rel="nofollow noreferrer">bio crate</a> for making reverse compliments of DNA strings, which <a href="https://github.com/rust-bio/rust-bio/blob/master/src/alphabets/dna.rs" rel="nofollow noreferrer">takes and returns a <code>Vec&lt;u8&gt;</code></a> (or at least returns <code>Vec&lt;u8&gt;</code>) so to avoid having to convert back and forth at some steps I want to keep them as such.</p>
  
  <p>Here is an example:</p>
  
  <pre><code>let dna_pieces = std::fs::read_to_string(path_dna_file).expect("Unable to read file");
  let dna_pieces = dna_pieces.split(" ");
  
  let mut dna_pieces_set = BTreeSet::new();
  
  // first adds a small set to the tree
  for dna_piece in dna_pieces {
      let dna_bytes = species_kmer.to_owned().into_bytes();
      dna_pieces_set.insert(dna_bytes);
  }
  
  // then adds a bigger other set to the same tree
  let dna_pieces_big_list = std::fs::read_to_string(path_dna_file_big).expect("Unable to read file");
  let dna_pieces_big_list = dna_pieces_big_list.split(" ");
  for dna_piece in dna_pieces_big_list {
      let dna_bytes = dna_piece.to_owned().into_bytes();
      let dna_bytes_to_rev = dna_piece.to_owned().into_bytes();
      let reverse_complement = bio::alphabets::dna::revcomp(dna_bytes_to_rev);
      if !dna_pieces_set.contains(&amp;reverse_complement) {
          dna_pieces_set.insert(dna_bytes);
      }
  }
  
  // format the treeset into a string output_unique_dna_pieces...
  
  std::fs::write(path_unique_dna_pieces, output_unique_dna_pieces).expect("Unable to write file");
  </code></pre>