title: How to get only the directory portion of the current executable's path?
body: <p>I want to read files from a config folder at the directory where the executable is located. I do that using the following functions:</p>
  
  <pre><code>use std::env;
  
  // add part of path to te path gotten from fn get_exe_path();
  fn get_file_path(path_to_file: &amp;str) -&gt; PathBuf {
      let final_path = match get_exe_path() {
          Ok(mut path) =&gt; {
              path.push(path_to_file);
              path
          }
          Err(err) =&gt; panic!("Path does not exists"),
      };
      final_path
  }
  
  // Get path to current executable
  fn get_exe_path() -&gt; Result&lt;PathBuf, io::Error&gt; {
      //std::env::current_exe()
      env::current_exe()
  }
  </code></pre>
  
  <p>In my case, <code>get_exe_path()</code> will return <code>C:\Users\User\Documents\Rust\Hangman\target\debug\Hangman.exe</code>.</p>
  
  <p>With <code>get_file_path("Config\test.txt")</code>, I want to append <code>Config\test.txt</code> To the above path. Then I get the following path to the file: <code>C:\Users\User\Documents\Rust\Hangman\target\debug\Hangman.exe\Config\test.txt</code></p>
  
  <p>The problem is that <code>std::env::current_exe()</code> will get the file name of the executable also and I do not need that. I only need the directory where it is located.</p>
  
  <p><strong>Question</strong></p>
  
  <p>The following the following function call should return <code>C:\Users\User\Documents\Rust\Hangman\target\debug\Config\test.txt</code>:</p>
  
  <pre><code>let path = get_file_path("Config\\test.txt");
  </code></pre>
  
  <p>How can I get the path from the current directory without the executable name like above example? Are there any other ways to do this than using <code>std::env::current_exe()</code></p>