Function serde::lib::core::env::current_exe

1.0.0 · source · []
pub fn current_exe() -> Result<PathBuf, Error>
Expand description

Returns the full filesystem path of the current running executable.

Platform-specific behavior

If the executable was invoked through a symbolic link, some platforms will return the path of the symbolic link and other platforms will return the path of the symbolic link’s target.

If the executable is renamed while it is running, platforms may return the path at the time it was loaded instead of the new path.


Acquiring the path of the current executable is a platform-specific operation that can fail for a good number of reasons. Some errors can include, but not be limited to, filesystem operations failing or general syscall failures.


The output of this function should not be used in anything that might have security implications. For example:

fn main() {
    println!("{:?}", std::env::current_exe());

On Linux systems, if this is compiled as foo:

$ rustc
$ ./foo

And you make a hard link of the program:

$ ln foo bar

When you run it, you won’t get the path of the original executable, you’ll get the path of the hard link:

$ ./bar

This sort of behavior has been known to lead to privilege escalation when used incorrectly.


use std::env;

match env::current_exe() {
    Ok(exe_path) => println!("Path of this executable is: {}",
    Err(e) => println!("failed to get current exe path: {e}"),