How do I duplicate a &[u8] slice?
<p>How do I copy the contents of a <code>&amp;[u8]</code> slice?</p>

<p>I'm trying to write a function that takes a buffer as input and XOR's
each byte with the given key and returns the final result.</p>

<p>I don't want it to mangle the input buffer.</p>

<pre><code>pub fn xor_buffer(buffer_in: &amp;[u8], key: char) -&gt; &amp;[u8] {

    let mut buffer_out = buffer_in.clone();

    for byte in &amp;mut buffer_out[..] {
        *byte ^= key as u8;
    }

    buffer_out
}
</code></pre>

<p>This code generates the following compile time error:</p>

<pre><code>src/test.rs:29:22: 29:32 error: cannot borrow immutable borrowed content `*buffer_out` as mutable
src/test.rs:29     for byte in &amp;mut buffer_out[..] {
                                               ^~~~~~~~~~
</code></pre>

<p>I know I have to be doing something wrong.</p>

<p>Any help would be apprecieated.</p>