Library Reference
Library Reference
Section titled “Library Reference”This page provides quick reference information for the main libraries used across the examples.
Candle
Section titled “Candle”Repository: huggingface/candle
A minimalist ML framework for Rust with focus on performance and ease of use.
Key Crates
Section titled “Key Crates”| Crate | Purpose |
|---|---|
candle-core |
Core tensor operations, device management, autograd |
candle-nn |
Neural network modules (layers, activations, etc.) |
candle-transformers |
Pre-built transformer models (BERT, GPT, T5, etc.) |
candle-examples |
Example utilities and helpers |
Common Patterns
Section titled “Common Patterns”// Device selectionlet device = Device::Cpu; // or Device::Cuda, Device::Metal
// Tensor creationlet tensor = Tensor::from_slice(&[1.0, 2.0, 3.0], (3,), &device)?;
// Model loadinglet model = Model::load(&model_path, &device)?;Documentation
Section titled “Documentation”Repository: rust-ml/linfa
A toolkit for classical machine learning in Rust, similar to scikit-learn.
Key Crates
Section titled “Key Crates”| Crate | Purpose |
|---|---|
linfa |
Core traits and data structures |
linfa-trees |
Decision trees, random forests |
linfa-linear |
Linear regression, logistic regression |
linfa-knn |
K-nearest neighbors |
linfa-clustering |
K-means, DBSCAN |
linfa-preprocessing |
Scaling, encoding, feature selection |
Common Patterns
Section titled “Common Patterns”// Dataset creationlet (train, test) = dataset.split_with_ratio(0.8);
// Model traininglet model = DecisionTree::params() .max_depth(Some(5)) .fit(&train)?;
// Predictionlet predictions = model.predict(&test);
// Evaluationlet cm = predictions.confusion_matrix(&test)?;Documentation
Section titled “Documentation”Tokenizers
Section titled “Tokenizers”Repository: huggingface/tokenizers
Fast, state-of-the-art tokenizers optimized for research and production.
Key Types
Section titled “Key Types”| Type | Purpose |
|---|---|
Tokenizer |
Main tokenizer interface |
Encoding |
Tokenized output (ids, tokens, offsets, etc.) |
Model |
Tokenization model (BPE, WordPiece, etc.) |
Common Patterns
Section titled “Common Patterns”// Load tokenizerlet tokenizer = Tokenizer::from_file("tokenizer.json")?;
// Encode textlet encoding = tokenizer.encode("Hello, world!", true)?;
// Access resultslet ids = encoding.get_ids();let tokens = encoding.get_tokens();Documentation
Section titled “Documentation”Hugging Face Hub
Section titled “Hugging Face Hub”Repository: huggingface/hf-hub
Rust client for the Hugging Face Hub.
Key Functions
Section titled “Key Functions”| Function | Purpose |
|---|---|
api.model(id) |
Get model repository |
api.dataset(id) |
Get dataset repository |
repo.get(file) |
Download a file |
repo.get_config() |
Get model config |
Common Patterns
Section titled “Common Patterns”let api = Api::new()?;let repo = api.model("bert-base-uncased".to_string());let config = repo.get_config()?;let model_file = repo.get("model.safetensors")?;Documentation
Section titled “Documentation”Repository: image-rs/image
Image processing library for Rust.
Key Types
Section titled “Key Types”| Type | Purpose |
|---|---|
DynamicImage |
Enum wrapping all image formats |
ImageBuffer |
Typed image buffer |
Pixel |
Pixel trait |
Common Patterns
Section titled “Common Patterns”// Load imagelet img = image::open("path/to/image.png")?;
// Resizelet resized = img.resize_exact(224, 224, FilterType::Triangle);
// Convert to RGBlet rgb = resized.to_rgb8();
// Access pixelsfor pixel in rgb.pixels() { // pixel is &Rgb<u8>}Documentation
Section titled “Documentation”Repository: clap-rs/clap
Command line argument parser for Rust.
Common Patterns
Section titled “Common Patterns”#[derive(Parser)]struct Args { #[arg(short, long)] input: String,
#[arg(short, long, default_value_t = 0.5)] threshold: f32,}
fn main() { let args = Args::parse(); // use args.input, args.threshold}