Common Standard Library Items
A reference guide to frequently used items from the Rust standard library.
Table of Contents
- Collections
- String Handling
- I/O Operations
- Filesystem Operations
- Networking
- Threading
- Time and Duration
- Memory Management
- Environment and Process
Collections
Vec<T> - Dynamic Array
use std::vec::Vec;
// Creation
let v: Vec<i32> = Vec::new();
let v = vec![1, 2, 3];
let v = Vec::with_capacity(10);
// Adding elements
v.push(4);
v.extend([5, 6, 7]);
v.insert(0, 0); // Insert at index
// Removing elements
v.pop(); // Returns Option<T>
v.remove(0); // Returns T, panics if out of bounds
v.clear(); // Remove all
// Accessing
let first = &v[0]; // Panics if out of bounds
let first = v.get(0); // Returns Option<&T>
let first = v.first(); // Returns Option<&T>
let last = v.last(); // Returns Option<&T>
// Iteration
for item in &v { } // Immutable iteration
for item in &mut v { } // Mutable iteration
for item in v { } // Consuming iteration
// Other operations
let len = v.len();
let is_empty = v.is_empty();
v.sort();
v.reverse();
v.retain(|x| x % 2 == 0); // Keep only even numbers