title: Extracting a file extension from a given path in Rust idiomatically
body: <p>I am trying to extract the extension of a file from a given String path.</p>
  
  <p>The following piece of code works, but I was wondering if there is a cleaner and more <strong>idiomatic Rust way</strong> to achieve this:</p>
  
  <pre><code>use std::path::Path;
  
  fn main() {
  
      fn get_extension_from_filename(filename: String) -&gt; String {
  
          //Change it to a canonical file path.
          let path = Path::new(&amp;filename).canonicalize().expect(
              "Expecting an existing filename",
          );
  
          let filepath = path.to_str();
          let name = filepath.unwrap().split('/');
          let names: Vec&lt;&amp;str&gt; = name.collect();
          let extension = names.last().expect("File extension can not be read.");
          let extens: Vec&lt;&amp;str&gt; = extension.split(".").collect();
  
          extens[1..(extens.len())].join(".").to_string()
      }
  
      assert_eq!(get_extension_from_filename("abc.tar.gz".to_string()) ,"tar.gz" );
      assert_eq!(get_extension_from_filename("abc..gz".to_string()) ,".gz" );
      assert_eq!(get_extension_from_filename("abc.gz".to_string()) , "gz");
  
  }
  </code></pre>