I have some sample code that is currently making use of getopts which is specified as a dependency in Cargo.toml
[dependencies]
getopts = "0.2"
However I can not seem to pass argument (-t
, --test
) through Cargo (e.g. cargo run --test
) for obvious reasons.
Since I have specified that external dependency, trying to run rustc src/main.rs --test
won't work either:
src/main.rs:2:5: 2:21 error: unresolved import `getopts::Options`. There is no `Options` in `getopts`
src/main.rs:2 use getopts::Options;
^~~~~~~~~~~~~~~~
error: aborting due to previous error
Is there another way to achieve this or some common alternative for the time being?
When no target selection options are given, cargo run will run the binary target. If there are multiple binary targets, you must pass a target flag to choose one. Or, the default-run field may be specified in the [package] section of Cargo. toml to choose the name of the binary to run by default.
Cargo is Rust's build system and package manager. Most Rustaceans use this tool to manage their Rust projects because Cargo handles a lot of tasks for you, such as building your code, downloading the libraries your code depends on, and building those libraries. (We call the libraries that your code needs dependencies.)
It extracts code samples from documentation comments of the library target, and then executes them. Different from normal test targets, each code block compiles to a doctest executable on the fly with rustc .
You can pass trailing arguments to cargo run
using --
:
cargo run -- --test
From the man
page:
All of the trailing arguments are passed to the binary to run. If you're passing arguments to both Cargo and the binary, the ones after
--
go to the binary, the ones before go to Cargo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With