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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response