title: How do I decide when to mark a function as unsafe?
body: <p>When is it appropriate to mark a function as <code>unsafe</code> versus just using an <code>unsafe</code> block? I saw this function while reading <a href="https://stackoverflow.com/a/29042896/155423">another answer</a>: </p>
  
  <pre><code>unsafe fn as_u8_slice(xs: &amp;[i32]) -&gt; &amp;[u8] {
      std::slice::from_raw_parts(v.as_ptr() as *const u8, 
                                 v.len() * std::mem::size_of::&lt;i32&gt;())
  }
  </code></pre>
  
  <p>I probably would have written the function as:</p>
  
  <pre><code>fn as_u8_slice(xs: &amp;[i32]) -&gt; &amp;[u8] {
      unsafe {
          std::slice::from_raw_parts(v.as_ptr() as *const u8, 
                                     v.len() * std::mem::size_of::&lt;i32&gt;())
      }
  }
  </code></pre>
  
  <p>That is, I feel like <em>calling the function</em> is safe in all cases, but what the function does <em>internally</em> cannot be verified by the compiler. However, I don't have any rules for when it is appropriate to use one or the other.</p>