title: Is there a way to modify how I'm using variables in a loop so that the order I'm initializing them does not matter?
body: <p>I'm fairly new to Rust, so not entirely sure how to properly title the question because I don't fully understand the error. I have the following simplified code which I'm using to parse command line arguments:</p>
  
  <pre><code>use std::env;
  
  fn main() {
  
      let mut script: &amp;str = "";
  
      // Get the commandline arguments.
      let args: Vec&lt;String&gt; = env::args().collect();
  
      // Loop through and handle the commandline arguments.
      // Skip the first argument; it's the name of the program.
      for arg in args.iter().skip(1) {
          let split: Vec&lt;&amp;str&gt; = arg.trim().split("=").collect();
  
          if split.len() == 2 {
              match split[0]{
                  "file" =&gt; { script = split[1]; }
                  _ =&gt; { println!("Invalid parameter: {}", arg); }
              }
          } else {
              println!("Invalid parameter: {}", arg);
              println!("Parameters should consist of a parameter name and value separated by '='");
          }
      }
  }
  </code></pre>
  
  <p>Which gives me the following error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error: `args` does not live long enough
    --&gt; src/main.rs:25:1
     |
  12 |     for arg in args.iter().skip(1) {
     |                ---- borrow occurs here
  ...
  25 | }
     | ^ `args` dropped here while still borrowed
     |
     = note: values in a scope are dropped in the opposite order they are created
  </code></pre>
  
  <p>When I change where the <em>script</em> variable is initialized:</p>
  
  <pre><code>use std::env;
  
  fn main() {
  
      // Get the commandline arguments.
      let args: Vec&lt;String&gt; = env::args().collect();
  
      let mut script: &amp;str = "";
  
      // Loop through and handle the commandline arguments.
      // Skip the first argument; it's the name of the program.
      for arg in args.iter().skip(1) {
          let split: Vec&lt;&amp;str&gt; = arg.trim().split("=").collect();
  
          if split.len() == 2 {
              match split[0]{
                  "file" =&gt; { script = split[1]; }
                  _ =&gt; { println!("Invalid parameter: {}", arg); }
              }
          } else {
              println!("Invalid parameter: {}", arg);
              println!("Parameters should consist of a parameter name and value separated by '='");
          }
      }
  }
  </code></pre>
  
  <p>The error goes away. Based on the error and how the order that the variables are being initialized changes things, I think I'm making a fundamental mistake in how I'm using (borrowing?) the variables in the loop, but I'm not entirely sure what I'm doing wrong and the proper way to fix it. Is there a way to modify how I'm using the variables in the loop so that the order I'm initializing them does not matter?</p>