Complete guide to Modules and Crates in Rust

Money Grows on Trees
2 min readAug 28, 2020
Photo by frank mckenna on Unsplash

Let’s familiarize ourselves with the Rust Terminology first

What is Cargo?

Cargo is Rust’s package manager; it helps to download package’s dependencies, compile packages, and publish them to crates.io

What is a Crate?

A crate is a library - it is the smallest compilation unit in Rust

What is a Module?

Rust by Example has a very succinct explanation of what modules are in Rust

How can I create a module?

Suppose I have a client.rs file and I want to create two sub-modules (signatures and kinto_http)

we need to create a folder named “client” that contains signatures.rs and kinto_http.rs

and add both sub-modules at the top of client.rs using mod keyword

You can follow the same logic for creating any number of sub-modules

What about setting visibility of modules (exposing certain structs/traits)

As you can see above, using the pub keyword we can mark client as a public module and using pub use we can expose certain members within the module for public use

--

--