title: Struggling with the subtyping relation of lifetimes in Rust
body: <p>I feel dumb for having browsed the <a href="http://doc.rust-lang.org/std/kinds/marker/index.html" rel="noreferrer">marker section</a> of the Rust documentation and the Wikipedia articles about <a href="https://en.wikipedia.org/wiki/Subtyping" rel="noreferrer">subtyping</a> and <a href="https://en.wikipedia.org/wiki/Covariance_and_contravariance_%28computer_science%29" rel="noreferrer">variance</a> multiple times without it improving my understanding of the lifetimes subtyping relation.</p>
  
  <p>I think I'm just used to the "typical OOP-style" subtying relations like "Cat &lt;: Animal" meaning "Cat is a subtype of Animal" where "S is a subtype of T" means "any term S can be safely used in a context where a term of type T is expected". So far so good.</p>
  
  <p>But how does this apply to lifetimes? The way it is <em>defined</em> right now in Rust is apparently(*)</p>
  
  <blockquote>
    <p>(#1)  'a &lt;: 'b  &lt;=>  lifetime a is no longer than lifetime b.</p>
  </blockquote>
  
  <p>And you might think "Of course that's what it means!" possibly because &lt;: looks similar to the less than operator or possibly because "sub" makes you think of subsets and a shorter lifespan is certainly a subset of a longer lifespan. But is 'a really a <em>subtype</em> of 'b if 'a is no longer than 'b? Let's try to apply Wikipedia's definition of the subtype relation:</p>
  
  <blockquote>
    <p>(#2)  'a &lt;: 'b  &lt;=>  lifetime a can be safely used in a context where lifetime b is expected.</p>
  </blockquote>
  
  <p>The problem I have is that I'm not able to reconcile this. How do you get from #2 to #1? Because to me, this seems like a contradiction... If you expect something to be alive for at least b and you have something with a lifetime a that's shorter than b, you obviously cannot use it in that context where something with a lifetime b is required, can you? Is it just me or did we get the subtyping relation for lifetimes wrong?</p>
  
  <p>Edit: (*) According to Ms2ger in the <code>#rust</code> IRC channel this is the case. It also fits with the documentation on the contravariant lifetime marker which is used in the <code>Items</code> iterator.</p>
  
  <p>Edit2: The ContravariantLifetime and CovariantLifetime markers have been removed. We now have <code>PhantomData</code> as a replacement in the <a href="https://doc.rust-lang.org/std/marker/index.html" rel="noreferrer">marker</a> module.</p>