title: What is the runtime complexity of Vec::len?
body: <p>Given a vector...</p>
  
  <pre><code>let v = vec![1, 2, 3, 4, 5];
  </code></pre>
  
  <p>Is calling <code>v.len()</code> O(1) or O(n)?</p>
  
  <hr>
  
  <p>Neither "The Book" (from what I can tell so far) nor <a href="https://doc.rust-lang.org/std/vec/struct.Vec.html#len.v" rel="noreferrer">the docs</a> mention whether <code>.len()</code> is constant time or not, and I cannot find anything on Stack Overflow or elsewhere.</p>
  
  <p>I'm assuming it's O(1) since <code>[]</code>, <code>.push()</code>, and <code>.pop()</code> <a href="https://doc.rust-lang.org/std/vec/" rel="noreferrer">all are</a>, but I want to be sure before I litter my code with <code>v.len()</code>.</p>
  
  <p>I know that I can easily just store/reference the return of <code>len</code> but in some situations - like inner functions - I don't want to keep having to pass both a vector <em>and</em> an int around.</p>
  
  <p>Thanks to @Stargateur for pointing out that indexing's O(1) is different from push/pop's <em>amortized</em> O(1)</p>