Algorithms
This category covers Rust implementations of core algorithms, such as sorting and random value generation, that are not covered in separate chapters.
Compression, cryptography, data structures, mathematics, scientific algorithms, search, strings, and text processing are discussed elsewhere but are briefly mentioned below.
Random Value Generation
Sorting
| Recipe | Crates | Categories |
|---|---|---|
| Sort a Vector of Integers | ||
| Sort a Vector of Floats | ||
| Sort a Vector of Structs |
Hashing
Related Topics
Compression Algorithms
For compression, use:
flate2for deflate, gzip, and zlib compression,zipfor zip files,tarfor tar archives,zstdfor Zstandard compression,bzip2,xz2,snapfor Snappy compression.brotlifor Brotli compression.
Data Structures
For common data structures (Vec↗, HashMap↗, etc.), you will use std::collections↗. You may also consider:
im↗ for immutable data structures,indexmapfor maps that keep track of insertion order,arrayvec,smallvecandtinyvecfor stack-allocated arrays,petgraph↗ for graphs,dashmapandpapayafor concurrent data structures,parking_lotfor mutexes,crossbeam-channel,flume,tokio, andpostagefor channels.
Worth a mention: fst provides fast, memory-efficient, immutable set and map data structures for storing and searching (very) large collections of strings and associated values. It is based on finite state transducers (FSTs). The capabilities of ordered sets and maps mirror that of BTreeSet and BTreeMap found in the standard library. The key difference is that sets and maps in fst are immutable, keys are byte sequences, and values, in the case of maps, are always unsigned 64 bit integers.
See vector hash maps, other maps, Binary Heap within the Data Structures section for more details.
Recursive Algorithms
stackeris a stack growth library useful when implementing deeply recursive algorithms that may accidentally blow the stack.
Search Algorithms
For small-scale tasks, like filtering a list of items in memory, reach first for the standard library:
-
slice::binary_search*for slices and vectors, -
std::iter::Iterator::findfor iterators, -
str::find,str::rfindfor strings. -
argminmaxprovides efficient argmin & argmax (in 1 function) with SIMD for floats and integers. -
bytecountcount occurrences of a given byte, or the number of UTF-8 code points, in a byte slice, fast.
meilisearch↗ and tantivy↗ are full-text search engines written in Rust, similar to what you'd expect from Elasticsearch or Algolia.
String Algorithms and Text Processing
For Strings, use:
regex↗ for regular expressions,fancy-regexif you need features, such as backtracking, which regex doesn't support,aho-corasick↗ for finding occurrences of many patterns at once,strsim↗ for string similarity.
See also String Concat, String Encoding, String Parsing, and Text Processing.
Numerical and Mathematical Algorithms
For Mathematics, use the following crates for general numerical tasks:
- Abstracting over different number types:
num-traits,num↗.
- Additional Numeric Types:
num-bigintandrugfor big integers.rust_decimalfor big decimals.ordered-floatfor sortable floats.
- Complex Numbers.
- Linear Algebra:
- Data frames:
polars.datafusion.
- Statistics,
- Trigonometry.
- Others
rustfftfor Fast Fourier Transforms.rootsfor numerical root finding.
References
rust-algorithms↗: Common data structures and algorithms in Rust.