I compiled a lib, how use it?

hi.

  1. I made a lib with ( cargo new lib delta )
  2. write code in src/lib.rs
  3. run cargo build

now, how use this lib inside another project ??

  • need to copy target files ??

A library is a crate (indicated by the Cargo.toml in the root directory). To use it, you must specify this crate as a dependency of the one you will use it from. If you want to use it locally, you can create a non-library crate and specify in the Cargo.toml :

[dependencies]
my_super_library = { path = "../path/to/my_super_library" }

You have all the details in the docs: Specifying Dependencies - The Cargo Book

1 Like

thanks. work great.

when i use that lib in my project like you said.
then when i compiled project and i want upload on somewhere

i need upload this lib seperate on same directory ??
or crate itself make a copies from that lib when done
cargo build ??

cargo compiles it again when used in another project. Rust uses static linking

2 Likes

The library itself is a separate package. If you want to have both in the same place, you can create a Cargo workspace which is in reality just a directory with a workspace file and one directory for each crate inside (one for your lib and one for your app).

1 Like

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.