[Issue?]Working directory changes `cargo run` and a built exe file
⚓ Rust 📅 2026-03-16 👤 surdeus 👁️ 3I develop Rust program for myself. File structure is like:
src/
-main.rs
data.txt
target/
-release/
--app.exe
and main.rs contains a code let file = File::open("data.txt").unwrap();
When cargo run, program can read "data.txt" but when running exe file which is built by cargo build --release, the app can't read "data.txt" because working directory is "target/release/".
Solution I think is using cargo run always not only when developing, but it takes time to compile and it may reflects wip code.
Another is, simply moving exe file to program root directory, but if I forget to move exe file when I build again, old exe file runs.
Is there a simple way to set working directory(value of std::env::current_dir()) same for cargo run and built exe file? Must I write codes which check path includes "target/release" and std::env::set_current_dir() for all program?
4 posts - 2 participants
🏷️ Rust_feed