Skip to main content

temp_dir

Function temp_dir 

1.0.0 · Source
pub fn temp_dir() -> PathBuf
Expand description

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 Android, there is no global temporary folder (it is usually allocated per-app), it will return the application’s cache dir if the program runs in application’s namespace and system version is Android 13 (or above), or /data/local/tmp otherwise.
  • On Darwin-based OSes (macOS, iOS, etc) it returns the directory provided by confstr(_CS_DARWIN_USER_TEMP_DIR, ...), as recommended by Apple’s security guidelines.
  • On all other unix-based OSes, it returns /tmp.

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());
}