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

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?

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

Difference between `cargo doc` and `cargo rustdoc`

According to doc.rust-lang.org
cargo rustdoc
build[s] a package's documentation, using specified custom flags
cargo doc
build[s] a package's documentation
What is the difference between the two? From what I understand cargo rustdoc is just like cargo doc, but it allows for more lints—for instance:
#![deny(rustdoc::broken_intra_doc_links)]
Is this correct? Oddly enough, cargo rustdoc will also fail in certain situations where cargo doc doesn't. For instance
some/folder on some-branch [$!] via 🦀 v1.60.0-nightly
❯ cargo doc
Finished dev [unoptimized + debuginfo] target(s) in 0.53s
some/folder on some-branch [$!] via 🦀 v1.60.0-nightly
❯ cargo rustdoc
error: manifest path `some/folder/Cargo.toml` is a virtual manifest, but this command requires running against an actual package in this workspace
Also, cargo doc does not support adding the -D option, whereas cargo rustdoc does.
❯ cargo doc -- -D rustdoc::broken_intra_doc_links
error: Found argument '-D' which wasn't expected, or isn't valid in this context
USAGE:
cargo doc [OPTIONS]
For more information try --help
Their relationship is like between cargo build and cargo rustc: cargo doc performs all the usual work, for an entire workspace, including dependencies (by default). cargo rustdoc allows you to pass flags directly to rustdoc, and only works for a single crate.
Here is the execution code for cargo rustdoc. Here is the code for cargo doc. The only differences is that cargo rustdoc always specify to not check dependencies while cargo doc allows you to choose (by default it does, but you can specify the flag --no-deps), and that cargo rustc allows you to pass flags directly to rustdoc with the flags after the --.

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

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

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

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