Add Rust library inside project directory not inside $HOME/.cargo/bin

When I add dependency in Cargo.toml file under dependencies field and run Cargo build command these dependencies are placed inside $HOME/.cargo/bin directory. I want them to be placed inside project directory. For example, if my directory is You_Name_It and all my code files are there including Cargo.toml and src then when I will add new library inside Cargo.toml I want it to be placed inside You_Name_It directory and nowhere else.

Is it possible to do that?

I'm newbie and please bear with me. Don't assume that I know something.

$HOME/.cargo/bin is for binaries installed using cargo install $cratename.
Dependencies in your Cargo.toml are library dependencies; their (compiled) artifacts will be (by defauilt) put into the target folder in your project and cargo knows how to tell the build process about it so they get linked in to your final project.

Adding a dependency to Cargo.toml will not suddenly put things into $HOME/.cargo/bin.
Maybe show what you did and which commands you ran so we can help you better.

Thanks for your answer. However, inside $HOME/.cargo/registry/scr/ I see libraries which I added in Cargo.toml file. Should they be there?
Also, under target/debug/build I see these libraries as well. Having library artefacts/anything here is Okay for me but why I see the libraries source under $HOME/.cargo/registry/scr/ ?

This is how my Cargo.toml file looks like

When I add new library, in the similar manner as rand = "0.8.4" then under $HOME/.cargo/registry/scr/ I see the source of that library. By source I mean git repo/source code.
I want that source code to be inside my project directory and not $HOME/.cargo/registry/scr/ here

The $HOME/.cargo directory is the default $CARGO_HOME. The cargo home directory is your download and source cache:

The "Cargo home" functions as a download and source cache. When building a crate, Cargo stores downloaded build dependencies in the Cargo home. You can alter the location of the Cargo home by setting the CARGO_HOME environmental variable. [...] By default, the Cargo home is located in $HOME/.cargo/.

I guess you could override $CARGO_HOME environment variable and set it to the you_name_it directory, if that is what you really want.

1 Like

$HOME/.cargo/registry/src is not $HOME/.cargo/bin.
So now that makes your misunderstanding more clear.

cargo keeps the registry, that is the index of crates that exist and the full source code of dependencies you need in a central location. That way it can update them and share that across projects when needed.
Building in your local project then builds those sources into artifacts put into your target folder.
See also the docs about Registries.

If you want to change where that registry is stored you can change CARGO_HOME (note that it moves everything that is currently $HOME/.cargo for you.
If you want to move where cargo puts produced artifacts you can set CARGO_TARGET_DIR.

If you really want to keep a local copy of your dependency's source code within your project look into cargo vendor.

2 Likes

@jer Thanks a lot for detailed explanation. I understand now how it works and I think it's much better not to touch that.

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.