Function temp_dir

fn temp_dir() -> PathBuf

Returns the path of a temporary directory.

The temporary directory may be shared among users, or between processes with different privileges; thus, the creation of any files or directories in the temporary directory must use a secure method to create a uniquely named file. Creating a file or directory with a fixed or predictable name may result in "insecure temporary file" security vulnerabilities. Consider using a crate that securely creates temporary files or directories.

Note that the returned value may be a symbolic link, not a directory.

Platform-specific behavior

On Unix, returns the value of the TMPDIR environment variable if it is set, otherwise the value is OS-specific:

On Windows, the behavior is equivalent to that of GetTempPath2 / GetTempPath, which this function uses internally. Specifically, for non-SYSTEM processes, the function checks for the following environment variables in order and returns the first path found:

  1. The path specified by the TMP environment variable.
  2. The path specified by the TEMP environment variable.
  3. The path specified by the USERPROFILE environment variable.
  4. The Windows directory.

When called from a process running as SYSTEM, GetTempPath2 returns C:\Windows\SystemTemp regardless of environment variables.

Note that, this may change in the future.

use std::env;

fn main() {
    let dir = env::temp_dir();
    println!("Temporary directory: {}", dir.display());
}