title: What is the optimal way to convert a `usize` to a `&str`?
body: <p>I need to convert a <code>usize</code> to a <code>&amp;str</code> to pass to a <code>fn foo(&amp;str)</code>. I found the following two ways, but don't know if there is a difference between using <code>as_str()</code> or <code>Deref</code>. Maybe the work done by <code>self</code> in <code>as_str</code> links with <code>Deref</code> somehow? I do not know if it may be better to use one or the other or if they actually are the same.</p>
  
  <ol>
  <li><p>Using <code>temp.to_string().as_str()</code>:</p>
  
  <pre><code>#[inline]
  #[stable(feature = "string_as_str", since = "1.7.0")]
  pub fn as_str(&amp;self) -&gt; &amp;str {
      self
  }
  </code></pre></li>
  <li><p>Using <code>&amp;*temp.to_string()</code> or <code>&amp;temp.to_string()</code>. This works via <code>Deref</code>:</p>
  
  <pre><code>#[stable(feature = "rust1", since = "1.0.0")]
  impl ops::Deref for String {
      type Target = str;
  
      #[inline]
      fn deref(&amp;self) -&gt; &amp;str {
          unsafe { str::from_utf8_unchecked(&amp;self.vec) }
      }
  }
  </code></pre></li>
  </ol>
  
  <blockquote>
    <p>The question may depend on what you want to do in foo: does the passed
    <code>&amp;str</code> need to outlive <code>foo</code> or not ?</p>
  </blockquote>
  
  <p><code>foo(&amp;str)</code> is a minimal example for <code>s: &amp;str</code> in this code:</p>
  
  <pre><code>extern crate termbox_sys as termbox;
  
  pub fn print(&amp;self, x: usize, y: usize, sty: Style, fg: Color, bg: Color, s: &amp;str) {
      let fg = Style::from_color(fg) | (sty &amp; style::TB_ATTRIB);
      let bg = Style::from_color(bg);
      for (i, ch) in s.chars().enumerate() {
          unsafe {
              self.change_cell(x + i, y, ch as u32, fg.bits(), bg.bits());
          }
      }
  }
  
  pub unsafe fn change_cell(&amp;self, x: usize, y: usize, ch: u32, fg: u16, bg: u16) {
      termbox::tb_change_cell(x as c_int, y as c_int, ch, fg, bg)
  }
  </code></pre>
  
  <p><a href="https://github.com/gchp/termbox-sys/blob/master/lib.rs" rel="noreferrer">termbox_sys</a></p>