Allowed Dependencies for Rust in AI Engineering from Scratch
The AI Engineering from Scratch curriculum enforces a strict stdlib-only policy for Rust, permitting only the standard library and prohibiting all external crates from crates.io.
This repository, rohitg00/ai-engineering-from-scratch, mandates that every Rust lesson must be implemented as a single-file program using only the allowed dependencies for Rust as defined in the project guidelines. According to the source code analysis of AGENTS.md (lines 57-62), learners must compile their solutions with rustc --edition 2021 without any Cargo.toml or third-party dependencies, ensuring that core machine learning concepts are built from first principles using native Rust APIs like std::collections and std::io.
The Stdlib-Only Dependency Policy
In AGENTS.md, the repository explicitly defines the allowed dependencies for Rust as stdlib-only. This means:
- No external crates: Packages from crates.io or GitHub dependencies are strictly forbidden.
- Single-file programs: Each lesson must consist of one
.rsfile that compiles directly with the Rust compiler. - Standard library only: Only modules within
std(such asstd::fs,std::vec, andstd::iter) may be used.
This restriction is designed to force implementations of algorithms—such as tensor operations, backpropagation, and tokenization—using only Rust's built-in capabilities rather than relying on pre-built machine learning libraries like ndarray or tch.
Why the Curriculum Restricts External Dependencies
The architectural decision to limit allowed dependencies for Rust serves three educational purposes:
- Educational clarity: By restricting code to the standard library, learners must engage directly with Rust's native APIs and manually implement core ML mathematics rather than calling abstracted library functions.
- Reproducibility: Single-file programs eliminate build-tool dependencies and versioning conflicts that typically arise with complex
Cargo.tomlconfigurations, ensuring that code runs identically across different environments. - Portability: Because the code relies only on
rustcwith the 2021 edition, programs compile and execute on any platform supporting the Rust toolchain without requiring dependency resolution or network access to crates.io.
Compliant Rust Code Examples
The following examples demonstrate valid patterns that adhere to the allowed dependencies for Rust policy. Each snippet uses only the standard library and compiles with rustc --edition 2021.
Vector Operations with Standard Library Iterators
This implementation calculates a dot product using iterator methods from std::iter, avoiding external linear algebra crates:
// main.rs – computes the dot product of two vectors using only std
use std::env;
fn dot_product(a: &[f64], b: &[f64]) -> f64 {
a.iter().zip(b.iter()).map(|(x, y)| x * y).sum()
}
fn main() {
// Example vectors; in a real lesson these could be read from stdin or a file
let v1 = vec![1.0, 2.0, 3.0];
let v2 = vec![4.0, 5.0, 6.0];
println!("dot = {}", dot_product(&v1, &v2));
}
Compile and run:
rustc --edition 2021 main.rs
./main
# → dot = 32
File I/O and Text Processing
This program demonstrates reading files and processing text using std::fs and std::process, complying with the restriction on allowed dependencies for Rust:
// main.rs – reads a file, splits on whitespace, counts tokens
use std::fs;
use std::process;
fn main() {
let path = "data.txt";
let contents = fs::read_to_string(path).unwrap_or_else(|err| {
eprintln!("Failed to read {path}: {err}");
process::exit(1);
});
let token_count = contents.split_whitespace().count();
println!("Token count: {}", token_count);
}
Manual Linear Algebra Implementation
The following snippet performs 1-D linear regression using primitive arithmetic and iterators, illustrating how statistical algorithms are implemented manually under the allowed dependencies for Rust constraints:
// main.rs – computes a 1‑D linear regression using only std
use std::ops::AddAssign;
fn mean(data: &[f64]) -> f64 {
data.iter().sum::<f64>() / data.len() as f64
}
fn variance(data: &[f64], mean: f64) -> f64 {
data.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / data.len() as f64
}
fn covariance(x: &[f64], y: &[f64], mean_x: f64, mean_y: f64) -> f64 {
x.iter()
.zip(y.iter())
.map(|(xi, yi)| (xi - mean_x) * (yi - mean_y))
.sum::<f64>()
/ x.len() as f64
}
fn main() {
let xs = vec![1.0, 2.0, 3.0, 4.0];
let ys = vec![2.0, 4.1, 5.9, 8.2];
let mx = mean(&xs);
let my = mean(&ys);
let b = covariance(&xs, &ys, mx, my) / variance(&xs, mx);
let a = my - b * mx;
println!("y = {:.3} + {:.3}x", a, b);
}
Real-World Implementations in the Repository
The curriculum demonstrates the practical application of these restrictions through several production-ready modules:
phases/10-llms-from-scratch/01-tokenizers/code/bpe.rs: Implements byte-pair encoding tokenization using only standard library string manipulation and file I/O operations.phases/10-llms-from-scratch/04-pre-training-mini-gpt/code/main.rs: Contains a minimal GPT-style transformer model built entirely withstdtypes, manual matrix operations, and custom random number generation.phases/10-llms-from-scratch/11-quantization/code/main.rs: Demonstrates quantization algorithms using primitive integer types and bitwise operations without relying on external numeric libraries.
These files prove that complex AI engineering concepts remain accessible when limited to the allowed dependencies for Rust specified in the curriculum guidelines.
Summary
- The allowed dependencies for Rust in AI Engineering from Scratch are restricted to the standard library only.
- All Rust code must compile as single-file programs using
rustc --edition 2021. - No external crates from crates.io or other registries are permitted.
- Learners implement core ML algorithms manually using
std::collections,std::iter, and primitive arithmetic. - The policy ensures reproducibility and portability across different development environments.
Frequently Asked Questions
Can I use Cargo.toml in AI Engineering from Scratch Rust lessons?
No. The curriculum explicitly prohibits Cargo.toml files and external crate management. All Rust lessons must be single-file programs that compile directly with rustc, as specified in AGENTS.md lines 57-62. This restriction ensures that learners focus on language fundamentals rather than dependency management.
What Rust edition is required for the curriculum?
All Rust code must be compiled with the 2021 edition. The specific compilation command is rustc --edition 2021, which enables modern Rust features while maintaining compatibility with the standard library APIs used throughout the lessons.
Are there any exceptions to the stdlib-only rule for Rust?
No exceptions are permitted. The allowed dependencies for Rust are strictly limited to the standard library across all phases of the curriculum, including advanced topics like LLM pre-training and quantization. Even for complex mathematical operations, you must implement solutions using std types rather than importing specialized crates.
How do I compile Rust files in this curriculum?
Navigate to your source file and run rustc --edition 2021 your_file.rs. This produces a binary executable in the same directory. Since no external dependencies are allowed, this single command handles compilation without requiring cargo build or internet connectivity to resolve crates.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →