Machine Learning in Rust: Libraries and Tools for Data Science
Rust is a systems programming language gaining popularity in Data Science and machine learning (ML). Thanks to its high performance, memory safety, and lack of a garbage collector, Rust is becoming an excellent alternative to Python for production systems where speed and reliability are critical. In this article, we will review the key libraries for machine learning in Rust, their capabilities, and usage examples.
Why Rust for Machine Learning?
Traditionally, ML development is done in Python due to its rich ecosystem (TensorFlow, PyTorch, scikit-learn). However, Python has limitations in execution speed and memory consumption. Rust offers:
- Performance on par with C/C++ — ideal for high-load inference and training on large datasets.
- Memory safety — the compiler prevents leaks and data races.
- Integration with Python — via PyO3, you can create fast Rust extensions for Python code.
- Embeddability — suitable for IoT, web services, and edge devices.
Main ML Libraries in Rust
1. Linfa — an analogue of scikit-learn
Linfa is a framework for classical machine learning algorithms. It provides a modular architecture where each algorithm is implemented in a separate crate. Supported features include:
- Linear and logistic regression
- Support Vector Machines (SVM)
- Decision trees and random forest
- Clustering (K-Means, DBSCAN)
- Dimensionality reduction (PCA)
Example: linear regression
use linfa::prelude::*;use linfa_linear::LinearRegression;use ndarray::Array2;
fn main() { // Training data: X (features), y (target variable) let x = Array2::from_shape_vec((5, 1), vec![1.0, 2.0, 3.0, 4.0, 5.0]).unwrap(); let y = Array2::from_shape_vec((5, 1), vec![2.0, 4.0, 6.0, 8.0, 10.0]).unwrap(); let dataset = Dataset::new(x, y); let model = LinearRegression::default().fit(&dataset).unwrap(); // Prediction let test_x = Array2::from_shape_vec((1, 1), vec![6.0]).unwrap(); let prediction = model.predict(&test_x); println!("Predicted value: {:?}", prediction);}2. Burn — neural networks and deep learning
Burn is a modern deep learning framework written in Rust. It supports:
- Automatic differentiation
- Various backends: CPU (ndarray), GPU (CUDA, Vulkan)
- Convolutional and recurrent networks
- Optimizers (Adam, SGD)
Example: a simple neural network
use burn::tensor::Tensor;use burn::nn::{Linear, Relu};use burn::optim::Adam;
fn main() { // Model definition let model = Linear::new(10, 2); let input = Tensor::random([1, 10], burn::tensor::Distribution::Normal); // Forward pass let output = model.forward(input); println!("Network output: {:?}", output);}3. SmartCore — full ML lifecycle
SmartCore is a library covering the entire pipeline: from data preprocessing to model evaluation. It includes:
- Normalization, feature scaling
- Classification (KNN, Naive Bayes)
- Regression (linear, ridge)
- Quality metrics (accuracy, F1-score)
4. Candle — lightweight framework from Hugging Face
Candle is a minimalist ML framework focused on inference. It is well-suited for running models on CPU and edge devices. It supports loading models in PyTorch format.