Rust Standard Library: Complete Overview
The Rust Standard Library (std) is a set of fundamental modules that provide developers with everything needed to create reliable, fast, and secure applications. Unlike many other languages, Rust places special emphasis on memory safety and concurrency without a garbage collector. In this article, we will examine the key components of std in detail with code examples.
1. Core Modules of the Standard Library
The library is divided into several logical groups. Let's look at the most important ones.
1.1. Module std::collections
Contains data structures for storing and managing collections of elements. The most popular ones:
Vec<T>— dynamic array (analogous toArrayListin Java).HashMap<K, V>— hash table for storing key-value pairs.HashSet<T>— set of unique values.BTreeMapandBTreeSet— sorted collections based on red-black tree.
use std::collections::HashMap;
fn main() { let mut scores = HashMap::new(); scores.insert(String::from("Alice"), 100); scores.insert(String::from("Bob"), 90);
for (name, score) in &scores { println!("{}: {}", name, score); }}1.2. Module std::io
Handles input/output: reading from keyboard, working with files, buffered input/output. Key traits: Read and Write.
use std::io::{self, BufRead};
fn main() -> io::Result<()> { let stdin = io::stdin(); for line in stdin.lock().lines() { println!("You entered: {}", line?); } Ok(())}1.3. Module std::thread
Allows creating and managing execution threads. Rust guarantees data safety between threads through the ownership system and the Send and Sync traits.
use std::thread;
fn main() { let handle = thread::spawn(|| { for i in 1..10 { println!("Thread: {}", i); } });
for i in 1..5 { println!("Main thread: {}", i); }
handle.join().unwrap();}2. Working with Strings and Text
Rust has two string types:
String— mutable, owning string (stored on the heap).&str— immutable string slice (reference to a UTF-8 sequence).
fn main() { let mut hello = String::from("Hello, "); hello.push_str("world!"); println!("{}", hello); // Output: Hello, world!
let slice: &str = &hello[0..6]; println!("Slice: {}", slice); // Output: Hello}3. Error Handling
Instead of exceptions, Rust uses two enums:
Option<T>— for cases when a value may be absent (SomeorNone).Result<T, E>— for operations that may fail (OkorErr).
use std::fs::File;use std::io::Read;
fn read_file(path: &str) -> Result<String, std::io::Error> { let mut file = File::open(path)?; let mut contents = String::new(); file.read_to_string(&mut contents)?; Ok(contents)}
fn main() { match read_file("test.txt") { Ok(content) => println!("Content: {}", content), Err(e) => eprintln!("Error: {}", e), }}4. Iterators and Closures
Iterators in Rust are lazy and powerful. They allow processing sequences of data without explicit loops.
fn main() { let numbers = vec![1, Also in library
Object Serialization in Node.js: JSON, Buffer and Protobuf
TypeBox for TypeScript: Complete Guide with Examples
Java Standard Library: A Complete Overview for Beginners and Pros
Authorization and Authentication in JavaScript (Node.js): A Complete Guide
Caching in Rust: Redis, Memcached, and Built-in Solutions