title: Is it possible to flatten Vec<&[u8]> into &[u8] without copying the data?
body: <p>I want to flatten a vector containing <code>&amp;[u8]</code>:</p>
  <pre><code>fn main() {
      let x: &amp;[u8] = &amp;b&quot;abc&quot;[..];
      let y: &amp;[u8] = &amp;b&quot;def&quot;[..];
      
      let z: Vec&lt;&amp;[u8]&gt; = vec![x, y];
      
      println!(&quot;z: {:?}&quot;, z);
      
      let z_flat: &amp;[u8] = z.into_iter().flatten().collect();
      
      println!(&quot;z_flat: {:?}&quot;, z_flat);
  }
  </code></pre>
  <p>I get the compiler error:</p>
  <pre class="lang-none prettyprint-override"><code>error[E0277]: a value of type `&amp;[u8]` cannot be built from an iterator over elements of type `&amp;u8`
   --&gt; src/main.rs:9:49
    |
  9 |     let z_flat: &amp;[u8] = z.into_iter().flatten().collect();
    |                                                 ^^^^^^^ value of type `&amp;[u8]` cannot be built from `std::iter::Iterator&lt;Item=&amp;u8&gt;`
    |
    = help: the trait `FromIterator&lt;&amp;u8&gt;` is not implemented for `&amp;[u8]`
  </code></pre>
  <p>I have also tried the following:</p>
  <pre><code>let z_flat: &amp;[u8] = &amp;z.into_iter().map(|x| x.to_vec()).flatten().collect();
  </code></pre>
  <p>Compile error:</p>
  <pre class="lang-none prettyprint-override"><code>error[E0277]: a value of type `[u8]` cannot be built from an iterator over elements of type `u8`
   --&gt; src/main.rs:9:70
    |
  9 |     let z_flat: &amp;[u8] = &amp;z.into_iter().map(|x| x.to_vec()).flatten().collect();
    |                                                                      ^^^^^^^ value of type `[u8]` cannot be built from `std::iter::Iterator&lt;Item=u8&gt;`
    |
    = help: the trait `FromIterator&lt;u8&gt;` is not implemented for `[u8]`
  </code></pre>
  <p>The following <strong>does</strong> work (<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=b834812fa23e9cf94e80e8ae0f8a2411" rel="nofollow noreferrer">playground</a>):</p>
  <pre><code>let z_flat: &amp;[u8] = &amp;z.concat();
  </code></pre>
  <p>I am not able to use <code>concat</code> in my real-world application because I am using another struct when retrieving the nested bytes (<code>x</code> and <code>y</code>) and this struct cannot implement <code>Copy</code> (only <code>Clone</code>) because it contains a <code>Vec&lt;u8&gt;</code> field. I have something similar to this (<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=f0a6a42d4f246d9a0381eafdd76d08a7" rel="nofollow noreferrer">playground</a>):</p>
  <pre><code>#[derive(Clone, Debug)]
  struct Foo {
      Bar: Vec&lt;u8&gt;,
  }
  
  impl Foo {
      fn baz(&amp;self) -&gt; Vec&lt;u8&gt; {
          self.Bar
      }
  }
  
  fn main() {
      let x = Foo { Bar: vec![8u8] };
      let y = Foo { Bar: vec![18u8] };
  
      let z: Vec&lt;Foo&gt; = vec![x, y];
  
      println!(&quot;z: {:?}&quot;, z);
  
      let z_flat = z
          .into_iter()
          .map(|x| &amp;x.baz()[..])
          .collect::&lt;Vec&lt;&amp;[u8]&gt;&gt;()
          .concat();
  
      println!(&quot;z_flat: {:?}&quot;, z_flat);
  }
  </code></pre>
  <p>Compile error:</p>
  <pre class="lang-none prettyprint-override"><code>error[E0507]: cannot move out of `self.Bar` which is behind a shared reference
   --&gt; src/main.rs:8:9
    |
  8 |         self.Bar
    |         ^^^^^^^^ move occurs because `self.Bar` has type `Vec&lt;u8&gt;`, which does not implement the `Copy` trait
  
  error[E0515]: cannot return value referencing temporary value
    --&gt; src/main.rs:22:18
     |
  22 |         .map(|x| &amp;x.baz()[..])
     |                  ^-------^^^^
     |                  ||
     |                  |temporary value created here
     |                  returns a value referencing data owned by the current function
  </code></pre>