External Libraries
An external library is a library that is outside of the src
directory (most likely in an entirely different project).
$ tree
.
├── my_library
│ ├── Cargo.toml
│ ├── Forc.toml
│ └─── src
│ └── lib.sw
│
└── my_other_library
├── Cargo.toml
├── Forc.toml
└─── src
└── lib.sw
Libraries
my_other_library
my_other_library
has a function quix()
which can be imported into my_library
because it uses the pub
keyword.
library;
pub fn quix() {}
my_library
To be able to use quix()
inside my_library
there are two steps to take.
Add to Dependencies
Add my_other_library
as a dependency under the [dependencies]
section of the my_library
Forc.toml
file.
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "lib.sw"
license = "Apache-2.0"
name = "my_library"
[dependencies]
my_other_library = { path = "../my_other_library" }
std = { path = "../../../../../../../../../sway-lib-std" }
Import
Use the use
keyword to selectively import our code from my_other_library
library;
use my_other_library::quix;
// `quix` from `my_other_library` is now available throughout the file