How do I run a project's example using Cargo? - rust

I'm trying to run the example code from this project. Following the instructions on the Cargo docs, I did the following:
git clone https://github.com/basiliscos/rust-procol-ftp-client
cd rust-procol-ftp-client
cargo run
cargo test
cargo test should also have compiled the example according to the Rust docs.
Although cargo test executes successfully, when I change into the target/debug directory, I don't find an executable for ftp-get (which is the example code). The target/debug/examples directory is also empty.
What is the best way to go about running this example?

You can run a specific example with:
cargo run --example name_of_example
where name_of_example is the base filename (without .rs)
or to run it in release mode:
cargo run --release --example name_of_example
To pass arguments to the example:
cargo run --example name_of_example -- arguments go here
cargo run will automatically build (or rebuild) the program first if it's out of date.

Try the following:
cd rust-procol-ftp-client
cargo build --examples
./target/debug/examples/ftp-get

Related

Specify --manifest-path while using cargo watch

A fantastic crate is cargo watch, which allows, for example, for you to execute:
cargo watch -x run
to automatically rebuild/run on src change. This is amazing for development but one issue I have is that it seems to not support the --manifest-path argument that can be used with cargo run to explicitly specify the path to the .toml file of the project such that it can be run from a different pwd than the Cargo.toml file itself:
cargo run --manifest-path /home/user/project/Cargo.toml
The crate documentation doesn't mention anything about this, so I was wondering if anyone who uses this crate has found a way around this. When attempting to use the --manifest-path argument I receive:
error: Found argument '--manifest-path' which wasn't expected, or isn't valid in this context
USAGE:
cargo watch [FLAGS] [OPTIONS]
Which makes some sense as I know not all commands support the --manifest-path arg, but since the crate uses cargo run to run the project itself I'm guessing there is some way around this without using e.g. sh -c 'cd [path to .toml file] && cargo watch -x run'
Yes you can just use the invocation which lets you run arbitrary commands:
cargo watch -- cargo run --manifest-path=path/to/Cargo.toml

cargo run --example exaple_name causes "no example target named 'example_name'"

I am studying the following code and cannot wrap my head around why cargo run --example abigen wouldn't work. The docs state that package has to be specified like so cargo run -p ethers --example abigen. Indeed, this fixes the issue. But according to Rust docs, I should be able to use the first version. Why doesn't this first version work?

error: a bin target must be available for `cargo run` [duplicate]

This question already has an answer here:
"a bin target must be available for 'cargo run'"
(1 answer)
Closed 3 years ago.
I am quite new using Rust language. I try to execute this cargo project/lib from github repository.
https://github.com/smallnest/benchpi
However, after cloning and run cargo run, I got this error error: a bin target must be available for 'cargo run'
How to properly run this cargo library? Thanks.
cargo run will look for a file called src/main.rs or src/bin/*.rs or some other file that's defined to be an application/binary in Cargo.toml. However, this project does not have one of these files. It is only a library with src/lib.rs. Without writing more code that calls the functions provided by this library, you can only run its unit tests and benchmarking suite.
You can run its unit tests on the latest Rust stable release by running cargo test. However, to run the benchmarks, you'll need to have the Rust nightly release installed. If you're using rustup to manage your Rust installation, you can install the nightly version of rust and use it to run the benchmarks like:
$ rustup install nightly
$ cargo +nightly bench

Is `cargo clippy` a superset of `cargo check`?

I'm trying to build and test my Rust code with a CI, and I'm wondering whether cargo clippy (potentially with options) covers everything that cargo check does. Do I only need to run cargo clippy, or do I need to run both?
clippy itself runs cargo check.

How to make Cargo run tests for local dependencies?

I'm working on a project split across multiple crates. The top-level crate (the app) requires the two other crates (libraries) as dependencies. Running cargo test in the top-level crate builds the dependencies and runs tests for the top-level crate, but it doesn't run tests for the two other crates. Is there a way to configure cargo test so that it will run tests in all three crates?
Thanks!
You can pass the -p parameter to make Cargo run the tests of a dependency.
So, if your crate is called sublib, you can run its tests using:
cargo test -p sublib
From cargo test --help:
-p SPEC, --package SPEC Package to run tests for
If the --package argument is given, then SPEC is a package id
specification which indicates which package should be tested. If it is
not given, then the current package is tested. For more information on
SPEC and its format, see the cargo help pkgid command.

Resources