title: Performance for safely handling out of range exceptions?
body: <p>I <a href="http://www.vadimbulavin.com/handling-out-of-bounds-exception/" rel="nofollow noreferrer">have an extension</a> of <code>Collection</code> that allows for safe access to elements without checking each time. This is what my extension looks like:</p>
  <pre><code>extension Collection {
  
      subscript(safe index: Index) -&gt; Element? {
          indices.contains(index) ? self[index] : nil
      }
  }
  </code></pre>
  <p>The time complexity of <code>indices.contains(index)</code> is <code>O(n)</code> which is a tough trade off. Is there a way to do this with <code>O(1)</code>?</p>