运行 当前目录之外的 Rust 程序
Running a Rust program that is outside of the current directory
当我当前不在包含程序的目录中时,如何执行 Rust 程序?如果我想在 Downloads/
目录中并且 运行 一个 Rust 文件在 Desktop/
目录中,我该如何使用 cargo run
?我以为我可以做类似 cargo run <path of rust file>
.
的事情
对于 运行 没有依赖关系的 Rust 文件,您可以在 shell/command 提示符下执行此操作。
转到如下所示的目录:
Directory
-> main.rs
运行
rustc ./main.rs
这将吐出 main
(Windows 上的 main.exe
),您可以 运行 通常为 ./main
(./main.exe
Windows)
如果你想使用 Cargo,你必须为它创建一个 Cargo.toml
并将 Rust 文件放入 src/
目录。
如果你不想和Rust文件在同一个目录下,那么你可以
rustc "<path/to/your/file>/main.rs"
您可以在 cargo run
上使用 --manifest-path
参数来指定 Cargo.toml
的路径(源文件将相对于该路径解析)。
例如:
$ cargo run --manifest-path ~/Desktop/src/myapp/Cargo.toml -- <program arguments>
请注意,如果您在包含 Cargo.toml
的目录上覆盖了 rustup 工具链,它将不 考虑在内。
当我当前不在包含程序的目录中时,如何执行 Rust 程序?如果我想在 Downloads/
目录中并且 运行 一个 Rust 文件在 Desktop/
目录中,我该如何使用 cargo run
?我以为我可以做类似 cargo run <path of rust file>
.
对于 运行 没有依赖关系的 Rust 文件,您可以在 shell/command 提示符下执行此操作。
转到如下所示的目录:
Directory
-> main.rs
运行
rustc ./main.rs
这将吐出 main
(Windows 上的 main.exe
),您可以 运行 通常为 ./main
(./main.exe
Windows)
如果你想使用 Cargo,你必须为它创建一个 Cargo.toml
并将 Rust 文件放入 src/
目录。
如果你不想和Rust文件在同一个目录下,那么你可以
rustc "<path/to/your/file>/main.rs"
您可以在 cargo run
上使用 --manifest-path
参数来指定 Cargo.toml
的路径(源文件将相对于该路径解析)。
例如:
$ cargo run --manifest-path ~/Desktop/src/myapp/Cargo.toml -- <program arguments>
请注意,如果您在包含 Cargo.toml
的目录上覆盖了 rustup 工具链,它将不 考虑在内。