Is it possible to change a dir?

Can system language Rust change the user's folder like Windows CMD command cd .. ?

This example (from you.com) freezes my pc:

use std::process::Command;

fn main() {
   let my_output = Command::new("cmd")
       .args(&["/C", "echo Hello, World!"])
       .output()
       .expect("failed to execute process");

   /* if my_output.status.success() {
       let my_result = String::from_utf8_lossy(&my_output.stdout);
       println!("Command output: {}", my_result);
   } else {
       let error = String::from_utf8_lossy(&my_output.stderr);
       println!("Command failed: {}", error);
   } */
}

I think you should use this.

A program can change the working dir for itself or its child processes, but it can't change the working dir of its parent process.

This means you generally can’t implement cd as an ordinary program, since any changes it makes to the working dir will have no effect after the program ends. The cd command is instead a built-in part of the shell or command-line environment.

11 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.