Problem installing clap

I'm totally new to rust but have a piece of code that I need to get working. It uses clap to parse the command line.

I have tries to install clap and these are the results:
1 - cargo install cargo-edit
Installed package cargo-edit v0.7.0 (executables cargo-add, cargo-rm, cargo-upgrade)

2 - cargo add clap
Command failed due to unhandled error: Unable to find Cargo.toml

3 - my .cargo directory looks like:

I have search the web extensively and have not found a single mention of this issue and, being a novice, I have no idea what to do to fix it.

Help would be greatly appreciated.

You need a cargo workspace to use cargo commands such as cargo-add.


cargo new foo
cd foo
cargo add clap

In which cargo new foo create a new cargo workspace with Cargo.toml file in foo directory.
You could see more frequently used cargo commands here: Rust Language Cheat Sheet

1 Like

Note the differences in the behaviour of cargo add and cargo install:

cargo add - is a third party extension to Rust's package manager Cargo. It is a convenience method in that you do not have to manually edit your Cargo.toml file for dependencies. It is similar to yarn add wherein the command does two things: a) it installs the dependency and updates the dependencies file (Cargo.toml in the case of Rust and package.json for JS).

cargo install is more of binary installation tool which builds and installs a binary application in an appropriate directory. You can read the rules for which directory here.

Therefore, as @tesuji suggests, you need to ensure there is a Rust project in the CWD with an appropriate Cargo.toml configured to use cargo add command.

Thanks for that info.

I'm thinking that I should have been in the project directory, rather than in my .cargo directory, when I ran the add command?
if so, maybe something useful has been omitted from the installation guide.

Yes, you need to be in your project directory when using the cargo add command.

if so, maybe something useful has been omitted from the installation guide.

If you think it can be helpful for others too, I am sure the team maintaining cargo-edit crate will welcome a PR updating the docs. :slight_smile:


Thanks for that link. I have been using the rust plugin to goland. It kind of blinded me to the cwd isue.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.