std::env::current_exe

Function std::env::current_exe

pub fn current_exe() -> Result<PathBuf>

Returns the full filesystem path of the current running executable.

The path returned is not necessarily a "real path" of the executable as there may be intermediate symlinks.

Errors

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.

Security

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.rs
$ ./foo
Ok("/home/alex/foo")

And you make a symbolic link of the program:

$ ln foo bar

When you run it, you won't get the original executable, you'll get the symlink:

$ ./bar
Ok("/home/alex/bar")

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

Examples

use std::env;

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

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/env/fn.current_exe.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部