Rust Standard Library: Complete Overview

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Rust Standard Library: Complete Overview



The Rust standard library (std) is the core of the language's ecosystem, providing developers with powerful and safe tools for building high-performance applications. Unlike many other languages, Rust places special emphasis on memory safety and zero-cost abstractions, making std not just a set of utilities but a foundation for writing reliable code. In this article, we will thoroughly examine the main modules of the standard library, their capabilities, and provide practical examples.



Main Modules of the Standard Library



The Rust standard library includes many modules covering almost all aspects of development. Let's look at the key ones:



  • std::collections — data structures (HashMap, Vec, BTreeMap, HashSet, etc.)
  • std::io — input/output (reading/writing files, working with the console)
  • std::fs — file system operations
  • std::thread — multithreading
  • std::sync — thread synchronization (Mutex, Arc, Barrier)
  • std::net — network programming (TCP, UDP)
  • std::path — working with file system paths
  • std::error — error handling


Working with Collections (std::collections)



The std::collections module provides efficient data structures. The most popular is Vec<T> (dynamic array). Also commonly used are HashMap<K, V> for storing key-value pairs and HashSet<T> for unique elements.



use std::collections::HashMap;

fn main() { let mut scores = HashMap::new(); scores.insert(String::from("Blue"), 10); scores.insert(String::from("Yellow"), 50);

// Access by key match scores.get(&"Blue".to_string()) { Some(score) => println!("Score: {}", score), None => println!("No score found"), }}


Input/Output and File Operations (std::io and std::fs)



The std::io and std::fs modules allow reading and writing files, working with console input/output. Rust guarantees safe file operations thanks to its ownership system.



use std::fs::File;use std::io::{BufRead, BufReader, Write};

fn main() -> std::io::Result<()> { // Writing to a file let mut file = File::create("example.txt")?; file.write_all(b"Hello, Rust!")?;

// Reading a file line by line let file = File::open("example.txt")?; let reader = BufReader::new(file); for line in reader.lines() { println!("{}", line?); } Ok(())}


Multithreading (std::thread)



Rust provides a safe multithreading model through the std::thread module. The compiler prevents data races at compile time thanks to ownership and borrowing rules.



use std::thread;use std::time::Duration;

fn main() { let handle = thread::spawn(|| { for i in 1..5 { println!("Thread: {}", i); thread::sleep(Duration::from_millis(100)); } });

for i in 1..3 { println!("Main thread: {}", i); thread::sleep(Duration::from_millis(50)); }

handle.join().unwrap();}


Synchronization (std::sync)



For safe data exchange between threads, primitives from std::sync are used: Mutex (mutual exclusion), Arc (atomic reference counting), and Barrier (synchronization barrier).



use std::sync::{Arc, Mutex};use std::thread;

fn main() { let counter = Arc::new(Mutex::new(0)); let mut handles = vec![];

for _ in 0..10 {\

Recommendations