title: How to modify a specific char in a string?
body: <p>I have a String and I want to update the value of a character in a specific index of the string. I know the index I'm going to update exists in the string because of my input type. My first idea was to get a mutable reference to the slice in the index I want, like this:</p>
  <pre class="lang-rust prettyprint-override"><code>let mut s = &quot;foo&quot;.to_owned();
  let index = s.get_mut(1..1).unwrap();
  *index = &quot;b&quot;;
  println!(&quot;{}&quot;, s);
  </code></pre>
  <p>but the compiler won't let me:</p>
  <pre class="lang-html prettyprint-override"><code>he size for values of type `str` cannot be known at compilation time
  
  doesn't have a size known at compile-time
  
  help: the trait `std::marker::Sized` is not implemented for `str`
  </code></pre>
  <p>What I could manage to do was change the exact byte using the unsafe <code>as_bytes_mut</code>:</p>
  <pre class="lang-rust prettyprint-override"><code>let mut s = &quot;foo&quot;.to_owned();
  unsafe {
    let bytes = s.as_bytes_mut();
    let index = &amp;mut bytes[2..3];
    'b'.encode_utf8(index);
  }
  println!(&quot;{}&quot;, s); // fob
  </code></pre>
  <p>Can I do it without using unsafe code though?</p>