From Rust to callable in a terminal?

I have a program I’ve written which I’d like to run from anywhere in the terminal. I’ve tried things like aliasing the target/release/ to something and calling that, similarly within a bash function but none of them work. Is there something I’m missing?

Also, not related to the question above but is there a way to include ~ in the file path in Rust? Similar to Python’s os.path.expanduser('~') or otherwise.

You need to put the binary in your PATH.

You can get the home directory using the home_dir function in https://crates.io/crates/dirs

3 Likes

There is also expanduser crate.

1 Like

You can run cargo install in your project, and it will install it “globally” with other cargo executables. That depends on having cargo’s bin directory in PATH, but if you’ve installed Rust that should already be the case.

2 Likes

To expand on how to get your binary into your PATH: PATH is an environment variable that contains a colon-separated list of directories where your shell looks for programs when you type their name. You can check which directories are in PATH by typing echo $PATH in your shell. If you put your binary in one of these, you can run it from the terminal. If you’ve setup cargo, then /home/yourname/.cargo/bin will already be in your PATH, and cargo install will put it there as @kornel said.

You can manually add any other directory to PATH using this shell command:

export PATH=/the/directory/you/want/to/add:$PATH

In order to make that persistent, add that line to your ~/.bashrc.