From the course: Advanced Rust: Managing Projects
The module system - Rust Tutorial
From the course: Advanced Rust: Managing Projects
The module system
- [Instructor] Short and simple programs, like the type you might create when first learning Rust, can reasonably live in a single source file. However, as your Rust programs grow in size and complexity, trying to store all that code in a single file becomes unreasonable. We need a way to divide up and organize large programs; facilitate the reuse of code across different applications; and control the scope and privacy of different parts of our code. Rust accomplishes those tasks through what's called the module system. Several key elements of the module system include, well, modules. We'll use modules to subdivide our code and group-related items to help organize it for readability and reuse. Modules provide isolated name spaces, allowing us to control the scope and privacy of items within a module. For example, let's say while creating my next great Rust application, I write a bunch of IO related functions. I might create separate modules to organize those functions based on their purpose to distinguish the functions for file.io from the ones for network.io. In addition to things like functions and macros, modules can also contain types, such as structs, enums, and unions. It can also contain traits and implementation blocks, constant and static values, and even other modules. To reference all of these items, we need the next feature of Rust's module system: paths, which are the way of naming and referring to items within the module system, so we can bring them into scope to use them in our program. Paths can be relative or absolute and we'll see examples of both later in this course. Our third element of the module system are crates, which come in one of two variations. A binary crate is an executable program meant to run on its own and a library crate represents a collection of related code or library, which is meant to be used as part of another program. Now, the term crate is used somewhat loosely and you may hear it used to refer to either the source code used to build an executable program or library, the compiled artifacts produced by the build process, or even a compressed package fetched from a registry, such as crates.io, which is the default registry in the Rust ecosystem for third party crates. Be prepared to hear the word crates used a lot in those various contexts. Finally, our last major feature of the Rust module system are packages, which are used to build, test, and share one or more crates that provide a set of functionality. A single package contains one or more targets, each of which is a crate. The package can have up to one library crate and zero to as many binary crates as you want as long as there's at least one crate. The package is organized with a directory structure, housing a collection of source files and a cargo.toml manifest file, which describes how to build the target crates, including information like external dependencies, the package name and version, and other necessary metadata. To summarize: our four key features of the module system are modules, paths, crates, and packages. Over the next few videos, we'll explore how these elements work together to build Rust programs.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
The module system3m 17s
-
Packages vs. crates8m 54s
-
(Locked)
Defining modules3m 3s
-
(Locked)
Absolute vs. relative path5m 52s
-
(Locked)
Public modules3m 37s
-
(Locked)
Public structs and enums4m 25s
-
(Locked)
Bringing paths into scope6m 3s
-
(Locked)
Using external crates5m 43s
-
(Locked)
Separating modules into multiple files6m 27s
-
(Locked)
Challenge: Organize a project2m 22s
-
(Locked)
Solution: Organize a project3m 54s
-
-
-