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

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

Related

How do I fetch and compile only the dev-dependencies? [duplicate]

This question already has answers here:
Can Cargo download and build dependencies without also building the application?
(6 answers)
Closed 10 months ago.
The Rust Cargo.toml specification file allows for development dependencies section, e.g. [dev-dependencies]
Cargo.toml:
[dev-dependencies]
tempdir = "0.3"
What is the command to fetch and compile those development dependencies?
To be clear, I want to fetch and compile only the [dev-dependencies]. The Linked questions differ in that they address fetching and compiling all dependencies and then building the application.
I do not have enough reputation so I just answer.
As declare by your link and rust-by-example, it is impossible to "build" a binary from dev-dependencies.
Sometimes there is a need to have dependencies for tests (or examples,
or benchmarks) only. Such dependencies are added to Cargo.toml in the
[dev-dependencies] section
To be sneaky, may be you can put some function under #[cfg(test)] and run cargo test so that your goal can be achieved.
A more appropriate way to selectively build by cargo using the feature flag, in cargo.toml and cargo command. You can take a look at the cargo command to toggle feature here and optional dependencies which help control categorizing of features.

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?

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 execute cargo test using the nightly channel?

I'm trying to run my tests with nightly Rust using Windows Powershell. I run cargo test in the directory, and I get
Compiling rustcraft v0.1.0 (file:///C:/Users/Phoenix/Desktop/Rust/rustcraft)
error[E0554]: #![feature] may not be used on the stable release channel
--> C:\Users\Phoenix\Desktop\Rust\rustcraft\src\main.rs:1:1
|
1 | #![feature(integer_atomics)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0554]: #![feature] may not be used on the stable release channel
--> C:\Users\Phoenix\Desktop\Rust\rustcraft\src\main.rs:2:1
|
2 | #![feature(collections)]
| ^^^^^^^^^^^^^^^^^^^^^^^^
Obviously, I have to tell Cargo to compile it on the nightly channel, but how? I can't find any reference to specifying a channel in the help section, or any website I've found.
The command line solution may help you to configure your IDE:
cargo +nightly test
Provided, of course, that you have the nightly channel installed. If not, perhaps install it with rustup install nightly (no need to switch to it, but check you're still on stable: rustup show).
The +<toolchain> functionality comes from rustup, the Rust toolchain manager. It works for both cargo +<toolchain> as well as rustc +<toolchain>.
In addition, you can use
rustup run <toolchain> <any arbitrary command goes here>
Since your project requires nightly features, you can change into the directory and run rustup override set <toolchain> to always use the nightly toolchain in that directory.
Create a file called rust-toolchain in your directory containing the name of the toolchain required (e.g. nightly). This has the safe effect as an override, but can be committed to source control.
See also:
Is it possible to have multiple coexisting Rust installations?

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