Header Ads

How to separate files in the rust module

 How to separate files in the rust module

How to separate files in the rust module

Maybe you are mixing the concepts of crates and modules. They are somewhat related but different. 

  1. Crates are separated units of compilation, distribution, versioning, and dependency management.
  2. Modules are pieces of a crate that are logically separated, with visibility barriers maybe also compiled separately, but finally linked together, and bundled into a crate.

Now we have three options for creating a separate file in the rust module:

1, Write module inline, It's means whole module same file.


2, Write module separate file same folder(source directory)


3, Write module in a subdirectory, "and the file name must be mod.rs"


4, So another way we can use Cargo for load library. Cargo is Rust package management so easy to load libraries.
We create two files main.rs and lib.rs for separate code.


My project to demo: https://github.com/dylanngo95/rust-module-example

Thanks.

No comments