when I place the exe in D drive and run this code, the read_dir result is the file list in the directory which exe lies, rather than the file list in D drive. How to fix it?
let read_path = Path::new("D:");
let dir_entries = fs::read_dir(read_path).unwrap();
for entry in dir_entries {
println!("{:?}", entry.unwrap().file_name());
}
The path D: specifies the drive, but not the folder inside that drive so it will default to the program's current working directory. Did you mean D:\ (i.e. the \ folder on the D drive)?
Technically "D:" will be the "drive-relative" current directory. This is pretty arcane (it dates back to early DOS) and only really a thing if you run programs via cmd.exe. So it can behave differently in different circumstances.
So yes the fix is to use "D:\\" or r#"D:\" (using a raw string).
Specifically, that's the cmd prompt shell, not "Windows" in general. The goal of cmd is backwards compatibility with DOS batch scripts. So cd will change the current directory of the specified drive, which may not be the current drive (unless you use the /d switch to change the drive as well as directory).
Explaining why this is so would involve exploring the history of DOS. Trivia: DOS 1.0 didn't even have subdirectories, just drives.
A path like "C:", "D:" or "Z:" is called a "drive-relative" path. It's relative to the current directory of the drive.
So if the current directory is D:\projects\rust then D: will be interpreted as D:\projects\rust. But if you use C: and there is no current directory for the "C" drive then it defaults to C:\.
You can try this in cmd.exe. If you type cd C: or cd D: it will tell you the current directory of that drive. Try switch between drives and setting different current directories.