Stdlib

29 articles
How to Build a Custom Parser in Rust Build a custom parser in Rust by defining a lexer to tokenize input, then implementing a recursive descent parser that consumes tokens to construct an Abstract Syntax Tree (AST). Use the `nom` crate for combinator-based parsing or write a manual parser using `match` expressions on token types. How to Build a TCP Client in Rust To build a TCP client in Rust, use the standard library's `std::net::TcpStream` to establish a connection and then wrap it in a `BufReader` and `BufWriter` for efficient buffered I/O. How to Build a TCP Server in Rust Build a basic TCP server in Rust using TcpListener to bind to a port and handle incoming connections. How to Build a UDP Server and Client in Rust Use `std::net::UdpSocket` to bind a server to an address and send/receive datagrams, while the client creates a `UdpSocket` to send data to the server's address. How to read environment variables Read environment variables in Rust using std::env::var to retrieve values or handle missing keys. How to Read Environment Variables in Rust Read environment variables in Rust using std::env::var to retrieve values or handle missing keys. How to Use nom for Parser Combinators in Rust Use the nom crate to build parsers by composing small functions that return results and handle input strings. How to Use pest for PEG Parsing in Rust Use the pest crate and pest_derive macro to define a grammar file and generate a PEG parser for Rust. How to Use std::cell for Interior Mutability Use `std::cell::RefCell<T>` to wrap data that needs to be mutated through an immutable reference by calling `borrow_mut()` to get a mutable reference at runtime. How to Use std::cmp for Ordering and Comparison Use std::cmp::Ordering with .cmp() and match to compare values and handle less, greater, or equal cases. How to Use std::convert (From, Into, TryFrom, TryInto) Implement From and Into for safe conversions, and TryFrom and TryInto for fallible conversions in Rust. How to Use std::env for Environment Interaction Read environment variables with std::env::var and command-line arguments with std::env::args in Rust. How to Use std::fmt for Custom Formatting Implement the Display trait to define custom string formatting for your Rust types. How to Use std::hash for Hashing Custom Types Implement the std::hash::Hash trait for your struct to enable its use as a key in HashMaps. How to Use std::marker Traits (Send, Sync, Copy, Sized) std::marker traits like Send, Sync, Copy, and Sized define how Rust types handle concurrency, duplication, and memory sizing. How to Use std::mem for Memory Operations Use std::mem functions like swap, replace, and size_of to manage memory layout and value exchange efficiently in Rust. How to Use std::net for Networking in Rust Use std::net::TcpListener to bind to a port and handle incoming TCP connections in Rust. How to Use std::num for Number-Related Traits (NonZero, Wrapping, etc.) Import types from std::num to enforce non-zero values or enable wrapping arithmetic safely. How to Use std::ops for Operator Overloading Overload operators in Rust by implementing specific traits from the std::ops module on your custom types. How to Use std::path for Path Manipulation Use std::path::Path for viewing paths and PathBuf for building or modifying them in a cross-platform way. How to Use std::process for Spawning Child Processes Spawn child processes in Rust using std::process::Command to execute external programs and capture their output. How to Use std::sync for Synchronization Primitives Use Arc and Mutex from std::sync to safely share mutable data across threads in Rust. How to Use std::time::Duration in Rust Use `std::time::Duration` to represent a span of time as nanoseconds, providing a type-safe way to handle timeouts, delays, and time arithmetic without manual unit conversion. How to Use std::time::Instant for Measuring Elapsed Time Use `std::time::Instant` to measure the duration of a code block by calling `Instant::now()` at the start and `.elapsed()` at the end, which returns a `Duration` object. How to Write a Crate-Level Documentation (//! comments) Write crate-level documentation by adding `//!` comments at the top of your library's root file. Tour of the Rust Standard Library: What's Available The Rust Standard Library offers built-in types and modules for data structures, I/O, concurrency, and more, accessible via std::. What Are the Standard Sections in Rust API Documentation? Rust API documentation includes summaries, descriptions, examples, and implementation lists generated via cargo doc. What Is in the Rust Prelude and Why? The Rust prelude automatically imports common types and functions like String and Vec into every program to reduce boilerplate. What Is the Prelude in Rust? The Rust Prelude is an automatically imported module containing common traits and types like Option and Result to reduce boilerplate.