How do I execute a Rust program when I am not currently inside the directory that holds the program? If I want to be in the Downloads/
directory and run a Rust file that is in the Desktop/
directory, how do I use cargo run
? I assumed I could do something like cargo run <path of rust file>
.
To run a Rust file without dependencies, you can do so on a shell/command prompt.
Go to directory that looks like so:
Directory
-> main.rs
Run
rustc ./main.rs
This will spit out main
( main.exe
on Windows) and you can run that normally as ./main
( ./main.exe
on Windows)
If you want to use Cargo, you'll have to create a Cargo.toml
for it and put the Rust file into the src/
directory.
If you want to not be in the same directory as the Rust file, then you can do
rustc "<path/to/your/file>/main.rs"
You can use the --manifest-path
argument on cargo run
to specify the path to Cargo.toml
(sources files will be resolved relative to that).
For example:
$ cargo run --manifest-path ~/Desktop/src/myapp/Cargo.toml -- <program arguments>
Note that if you have a rustup toolchain override on the directory containing Cargo.toml
, it will not be taken into account.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.