Machine Learning in Rust: Modern Libraries and Tools
The machine learning ecosystem has long been associated exclusively with Python. However, Rust, thanks to its performance, memory safety, and zero-cost abstractions, is increasingly being adopted in ML tooling. In this article, we will review the key libraries for machine learning in the Rust language that allow you to build, train, and deploy models with efficiency unattainable for interpreted languages.
Why Rust for Machine Learning?
Rust offers three main advantages for ML:
- Performance — code execution speed comparable to C/C++.
- Safety — the ownership system eliminates entire classes of memory errors.
- Concurrency — a built-in threading model without data races.
However, Rust does not attempt to replace Python in the research phase. Its niche is production environments, embedded systems, high-load services, and edge computing.
1. Linfa — A Complete Set of Classical ML Algorithms
The linfa project (an analog of scikit-learn for Rust) provides a modular structure for supervised and unsupervised learning tasks. The library is actively developed with support from the Rust-ML community.
Main Components of Linfa
linfa-clustering— K-Means, DBSCAN, Gaussian Mixturelinfa-linear— Linear and Logistic Regressionlinfa-svm— Support Vector Machineslinfa-trees— Decision Trees and Random Forestlinfa-nn— Nearest Neighbor Methods
Example: Linear Regression with Linfa
use linfa::prelude::*;use linfa_linear::LinearRegression;use ndarray::array;
fn main() { // Training data: (x, y) let x = array![[1.0], [2.0], [3.0], [4.0]]; let y = array![2.0, 4.0, 6.0, 8.0];
// Create dataset let dataset = Dataset::new(x, y);
// Train the model let model = LinearRegression::default().fit(&dataset).unwrap();
// Prediction for x=5 let pred = model.predict(&array![[5.0]]); println!("Prediction: {}", pred);}2. Burn — Deep Learning Framework with GPU Support
Burn is a modern framework for neural networks written in Rust. It supports automatic differentiation, GPU computation via WGPU and Vulkan, and export to ONNX.
Key Features of Burn
- Dynamic and static computation graphs
- Backends: Torch, WGPU, Ndarray, Fusion
- Built-in optimizers (Adam, SGD, AdamW)
- Support for 1D, 2D, 3D convolutions
Example: Simple Perceptron in Burn
use burn::tensor::{Tensor, backend::Backend};use burn::nn::{Linear, LinearConfig};use burn::module::Module;
#[derive(Module)]struct MLP<B: Backend> { layer1: Linear<B>, layer2: Linear<B>,}
impl<B: Backend> MLP<B> { fn new(device: &B::Device) -> Self { Self { layer1: LinearConfig::new(2, 16).init(device), layer2: LinearConfig::new(16, 1).init(device), } }
fn forward(&self, input: Tensor<B, 2>) -> Tensor<B, 2> { let x = self.layer1.forward(input).relu(); self.layer2.forward(x) }}3. Candle — Minimalist Framework from Hugging Face
Developed by the Hugging Face team, Candle is focused on simplicity and speed. It does not use CUDA (only CPU and Metal), making it ideal for inference on Mac and edge devices.
Features of Candle
- Binary size less than 10 MB
- Built-in support for models: LLaMA, Whisper, BERT
Also in library
Geolocation and Maps in PHP: Overview of Libraries and Tools
Go (Golang) Application Monitoring: Libraries and Tools
Logging in Kotlin: Best Libraries and Tools for Development
Working with Excel in JavaScript (Node.js): Libraries and Examples
Working with Excel in PHP: PhpSpreadsheet and OpenSpout Libraries