How to find the current version of a Rust library? - rust

The Cargo.toml file requires me to state the version of a dependency, e.g. rand = "0.6".
I want to use the package rand_pcg, but don't know the version. How may I find it?

Use the web
crates.io
Navigate to https://crates.io/, type your crate name into the search box, and see the version. You can also click the clipboard icon to copy the complete dependency to add to Cargo.toml.
docs.rs
Navigate to https://docs.rs/, type your crate name into the search box, and see the version. If you click through to the crate, you can then click the clipboard icon to copy the complete dependency to add to Cargo.toml.
lib.rs
Navigate to https://lib.rs/, type your crate name into the search box, and see the version. If you click through to the crate, you can then click on the "installation" tab to see the complete dependency to add to Cargo.toml.
Use the command line
cargo build
Add the wildcard dependency to your Cargo.toml (e.g. rand_pcg = "*"). Run cargo build and note the version it picked (e.g. Compiling rand_pcg v...) or look in Cargo.lock for the entry for the crate. Edit Cargo.toml to use this version.
cargo add
Install cargo edit then run cargo add rand_pcg. This is my preferred route.
See Is there a command to automatically add a crate to my Cargo.toml? for more.
cargo search
As mentioned by user2722968, you can run cargo search rand-pcg and it will output the dependency line.

Related

Can we use my own crate repository url in Cargo.toml to download the dependencies

I have my own repository, where I have list of rust crates. When I run the command "cargo run", i want cargo to download all the dependencies from my crate repository url instead of crates.io. To achieve this what configuration need to be done
Looking for some suggestion or code snippet for the same

How to force --document-private-items from Cargo.toml?

Is there a way of forcing private items documentation on docs build?
It can be done manually adding the doc flag --document-private-items, but I would like to force it in Cargo.toml level for example.
You can do this not with Cargo.toml but cargo's config. In your project (or in $CARGO_HOME if you want this to apply for all of your projects) create .cargo/config.toml with following contents:
[build]
rustdocflags = ["--document-private-items"]
Here is the link to the documentation.

What the difference between Cargo.toml and .cargo/config.toml

I am learning to write a kernel by using Rust.
I added [dependencies] bootloader = "0.9.8" to .cargo/config.toml, but I got an error.
I forgot the specific error, but when I move [dependencies] bootloader = "0.9.8" to Cargo.toml and then run "cargo bootimage" on terminal, everything goes well.
Cargo.toml is where you put everything your project need. Dependencies, project details, project settings etc..
cargo/config.toml is a configuration file for Cargo. You usually don't need to touch it, and it usually uses more advanced things. It can be configured per project, although also globally. It sets for example what commands Cargo will run, what environment variables it will set, etc..
All details about both are in the documentation - Cargo.toml, config.toml.

Adding Pallets to Substrate

This is a very basic question, but I am following the "nicks" tutorial from the Substrate network, and it's not very clear how - after I have added the pallet - I install or "pull in" these dependencies so I can inspect the code. I was expecting something like node_modules folder where everything is put once you run npm i, but that is a different environment. I have run cargo build but to no avail. Please advise.
When you add dependency in Cargo.toml, by default, the actual dependency will download and store on global Cargo registry $HOME/.cargo/ and specify the version in Cargo.lock inside project directory, not the actual dependency.
For a tutorial on Substrate, after adding Nick's pallet and running cargo build, you can check the code of downloaded pallet_nick in $HOME/.cargo/. The easiest way is to download rust_analyzer plugin to your VSCode and CRTL + Click to the dependency name inside Cargo.toml, it will take you to the library.

What does "manifest path is a virtual manifest, but this command requires running against an actual package" mean?

I'm trying to build a Rust project (xray). When running cargo run I get the following error message
error: manifest path D:\xray\building\xray\Cargo.toml is a virtual
manifest, but this command requires running against an actual package
in this workspace
What exactly does this mean and how can it be solved? I'm using Cargo version 0.25.0 and Rust version 1.24.1.
Your Cargo.toml is a virtual manifest.
In workspace manifests, if the package table is present, the workspace root crate will be treated as a normal package, as well as a workspace. If the package table is not present in a workspace manifest, it is called a virtual manifest.
When working with virtual manifests, package-related cargo commands, like cargo build, won't be available anymore. But, most of such commands support the --all option, will execute the command for all the non-virtual manifest in the workspace.
cargo run does not work, because cargo doesn't know what to run. There are two options:
--manifest-path <PATH>: Path to Cargo.toml of the crate you want to run.
-p, --package <SPEC>: Package you want to run.
In your case it's probably cargo run --package xray_cli
the manifest has both package and workspace sections can't works. please check the Cargo.toml and remove package from it.
Virtual Manifest is new concepts, please reading docs to familiar it. Hope it values for you.

Resources