Cargo is configured to look for dependencies on crates.io by defalut. However I want to use a local crate. Fortunately, cargo supports to use libraries ont only on crates.io, but also ther registries, git
repositories or subdirectories on our local file system.
create a lib
Firstly, create a new package:
1 | $ cargo new test_crate --lib |
Here we pass --lib
because we’re making a library. Then the cargo generates:
1 | . |
Let’s do something in lib.rs
.
1 | pub fn public_function_in_test_crate() { |
set Cargo.toml
Then we create a new binary program use cargo new test_extern_crate
and add a dependency section to our executable’s Cargo.toml
and sepcify the path:
1 | [dependencies] |
or
1 | [dependencies.test_crate] |
Now we can use our local crate test_crate
as folliwing:
main.rs:
1 | extern crate test_crate; |
more details in more detals in Cargo book .