title: How do I insert a dynamic byte string into a vector?
body: <p>I need to create packet to send to the server. For this purpose I use vector with <code>byteorder</code> crate. When I try to append string, Rust compiler tells I use unsafe function and give me an error.</p>
  <pre><code>use byteorder::{LittleEndian, WriteBytesExt};
  
  fn main () {
      let login = &quot;test&quot;;
      let packet_length = 30 + (login.len() as i16);
  
      let mut packet = Vec::new();
      packet.write_u8(0x00);
      packet.write_i16::&lt;LittleEndian&gt;(packet_length);
      packet.append(&amp;mut Vec::from(String::from(&quot;game name &quot;).as_bytes_mut()));
  
      // ... rest code
  }
  </code></pre>
  <p>The error is:</p>
  <pre class="lang-none prettyprint-override"><code>packet.append(&amp;mut Vec::from(String::from(&quot;game name &quot;).as_bytes_mut()));
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function
  </code></pre>
  <p>This is playground to reproduce: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2021&amp;gist=381c6d14660d47beaece15d068b3dc6a" rel="nofollow noreferrer">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2021&amp;gist=381c6d14660d47beaece15d068b3dc6a</a></p>
  <p>What is the correct way to insert some string as bytes into vector ?</p>