Course contentsShow
← Rust
Lesson 20 of 1,9101. Rust Fundamentals and ToolingFree lesson

Standard Library Layout

Explore the modular organization of the standard library to locate essential primitives and types.

In this lesson, you will learn how the Rust standard library is organized so you can quickly navigate and find the tools you need for your programs.

The Geography of std

When you write Rust code, you are working with the Standard Library (often referred to as std). You don't need to add it to your Cargo.toml because it is automatically available to every Rust project. However, it is vast, so it is structured into logical modules to keep it manageable.

Think of std as a massive toolbox. If every tool were thrown into one giant pile, you’d never find anything. Instead, the library is grouped by functionality.

Core Modules

Most things you use daily are organized into these common categories:

  • std::primitive: Contains the built-in types like i32, bool, and char. You rarely need to call these explicitly, as they are part of the language syntax.
  • std::collections: This is where you find data structures that hold multiple items, such as Vec (a dynamic array) or HashMap (a key-value store).
  • std::fs and std::io: These modules handle file system operations and reading/writing data streams (like reading from a terminal or a file).
  • std::convert: Contains tools for changing one type into another.

How to Access Them

Because the standard library is so large, Rust does not import every single module into your file by default. This keeps your program's compilation time fast. To use a specific tool, you use the use keyword to bring it into your scope:

use std::collections::HashMap; // Importing a specific collection
use std::fs::File;            // Importing file system tools

fn main() {
    let mut map = HashMap::new();
    // Now you can use the HashMap tools
}

By looking at the path—for example, std::fs::File—you can decode the layout: the root is std, the category is fs, and the specific tool is File. When you are searching for a feature, remember to check the documentation for the module category first. If you need to store data, look in collections; if you need to talk to the hardware, look in io or fs.

Key Takeaway: The standard library is a modular hierarchy where tools are grouped by their functionality, and you explicitly import the modules you need to keep your code clean and efficient.