I'm writing a command-line tool using Rust. I want to cd
to the wanted dictionary when I execute my Rust command-line tool.
I use env::set_current_dir(path)
, but it does not work. After that, I use nix
, call nix::unistd::chdir(path)
, but it does not work either.
So how can I archieve the goal of chaging command dictionary? How to make cd
call in Rust.
This is not directly possible - the Rust program can only affect its own environment, not your shell. You can define a shell function which uses the output of your Rust program to perform some action, for example
# bash
mycd() {
cd "$(command myrustprogram)"
}
// main.rs
fn main() {
println!("/some/path")
}