title: How to analyze generic lifetime when it is relevant to variance
body: <pre class="lang-rust prettyprint-override"><code> 0 // code snippet 1
   1
   2 struct MutStr&lt;'a &gt;{
   3     s: &amp;'a mut &amp;'a str,
   4 }
   5 
   6 fn main() {
   7     let mut s: &amp;'static str = &quot;hello&quot;;
   8     *MutStr{
   9         s: &amp;mut s,
  10     }.s = &quot;world&quot;;
  11     println!(&quot;{}&quot;, s);
  12 }
  </code></pre>
  <blockquote>
  <p>Rust Playground <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2021&amp;gist=1e8396c749ae5dc4c20f4407d0d68d61" rel="nofollow noreferrer">link</a> of <code>code snippet 1</code></p>
  </blockquote>
  <p>The <code>code snippnet 1</code> above varies from <a href="https://rust-for-rustaceans.com" rel="nofollow noreferrer">Rust for Rustacean</a> Ch1 listing 1-11 where I use <code>'a</code> to replace two lifetimes <code>'a</code> and <code>'b</code> , and this code can not compile, whereas I can not tell the reason:(</p>
  <p>I can analyze some simple code such as the following one (from Programming Rust Verson 2 page 120):</p>
  <pre class="lang-rust prettyprint-override"><code> 0 // code snippet 2
   1 
   2 struct S&lt;'a&gt; {
   3     x: &amp;'a i32,
   4     y: &amp;'a i32
   5 }
   6
   7 fn main() {
   8     let x = 10;
   9     let r;
  10     {
  11         let y = 20;
  12         {
  13             let s = S {x: &amp;x, y: &amp;y};
  14             r = s.x;
  15         }
  16     }
  17     println!(&quot;{}&quot;, r);
  18 }
  </code></pre>
  <blockquote>
  <p>Rust Playground <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2021&amp;gist=c07b6f31259a222c2b39aaa721b4715e" rel="nofollow noreferrer">link</a> of <code>code snippet 2</code></p>
  </blockquote>
  <p>I denote lifetime of <code>x</code>, <code>y</code> and <code>r</code> with <code>'x</code> ,  <code>'y</code> and <code>'r</code> respectively:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: center;">'x</th>
  <th style="text-align: center;">'y</th>
  <th style="text-align: center;">'r</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: center;">line: from 8 to 18</td>
  <td style="text-align: center;">line: from 11 to 16</td>
  <td style="text-align: center;">line: from 9 to 17</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>When  instantiating <code>s</code> in line <code>13</code>,  we are requiring <code>'x:'a ('x outlives 'a)</code> <code>'y:'a</code> , and when assignment <code>r = s.x</code> happens in line <code>14</code> , we are requiring <code>'a:'r</code> . Nevertheless, this is impossible for the reason that <code>'y:'a</code> conflicts with <code>'a:'r</code> (see table above, <code>'y</code> is shorter than <code>'r</code> ) so that rustc can not find a deterministic lifetime for generic lifetime <code>'a</code> satisfying those conflicting conditions.</p>
  <p>Update: I am expecting a analyze process for  <code>code snippet 1</code> similar to the above one, or a general inference method when encountering lifetime problems:)</p>
  <p>I have read <a href="https://medium.com/nearprotocol/understanding-rust-lifetimes-e813bcd405fa" rel="nofollow noreferrer">this article</a> , knowing some basic concepts about <code>variance</code>, such as <code>covariance</code>  <code>invariance</code> and <code>contravariance</code> . And I kind of think my question relate to that, but don't know how to use that to analyze <code>code snippet 1</code>.</p>
  <p>By the way, I am still wondering what is the right position to master <code>lifetime</code> in rust. If you know, please tell me:) , I do appreciate that.</p>
  <blockquote>
  <p>If you find my question irregular, feel free to point it out:)</p>
  </blockquote>