Rust function to generate a temporary filename without creating file

Hello! Is there a function that will generate a temporary filename inside TMPDIR without actually creating the file? Julia for instance has the tempname function (see docs). Is there a rust equivalent?

The problem with attempting this is that the file name may be claimed by another program between the time it is chosen and the time you create the file. If you need to choose a file name in advance (e.g. to instruct a subprocess to write to it), it is safer to either create the file using a temporary file routine and then overwrite it, or to create an empty temporary directory and create a file inside it.

3 Likes

Ah I see! That makes sense. I’m actually using git2-rs to create a worktree in a temporary directory, but the crate itself internally tries to allocate directory, and errors out even if the provided directory is empty. So my second attempt was to create a path only.