Skip to content

Setup

This guide sets up your Rust environment for the AI/ML in Rust tutorial, installing Rust, key ML libraries, and verifying with a simple program. Basic Rust familiarity is helpful, but no ML experience is needed.

Step 1: Install Rust

  1. Visit rust-lang.org.
  2. Install rustup: On Unix-like systems (macOS/Linux), run:
    bash
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    On Windows, follow the website’s instructions.
  3. Choose default options during installation.
  4. Update Rust:
    bash
    rustup update
  5. Verify:
    bash
    rustc --version
    cargo --version
    Expect versions like rustc 1.86.0, cargo 1.86.0.

Step 2: Set Up an IDE

Use Visual Studio Code (VS Code) for coding efficiency:

  1. Install VS Code: Download from code.visualstudio.com.
  2. Add rust-analyzer: In VS Code Extensions (Ctrl+Shift+X), install rust-analyzer for code completion and diagnostics.

Step 3: Create a Rust Project

  1. Create project:
    bash
    cargo new rust_ml_tutorial
    cd rust_ml_tutorial
  2. Test:
    bash
    cargo run
    Expect “Hello, world!” output.

Step 4: Install ML Libraries

Add these to Cargo.toml under [dependencies]:

toml
linfa = "0.7.1"
tch = "0.17.0"
polars = { version = "0.46.0", features = ["lazy"] }
nalgebra = "0.33.2"
rust-bert = "0.23.0"
actix-web = "4.4.0"

Note: Ensure tch version matches rust-bert’s requirements to avoid conflicts; check github.com/guillaume-be/rust-bert. Run:

bash
cargo build

Library Overview

  • linfa: Regression, clustering (Core ML).
  • tch-rs: Neural networks (Deep Learning).
  • polars: Data preprocessing (Practical ML Skills).
  • nalgebra: Matrix operations (Mathematical Foundations).
  • rust-bert: NLP tasks (Advanced Topics).
  • actix-web: Model deployment (Practical ML Skills).

Step 5: Verify Setup

Test with a nalgebra matrix operation in src/main.rs:

rust
use nalgebra::Matrix2;

fn main() {
    let matrix = Matrix2::new(1.0, 2.0, 3.0, 4.0);
    println!("Matrix:\n{}", matrix);
}

Run:

bash
cargo run

Troubleshooting

  • Rust issues: Reinstall via rust-lang.org.
  • Dependency errors: Run cargo clean and cargo build.
  • IDE issues: Reinstall rust-analyzer.

Next Steps

Continue to Rust Basics or First ML Lab.

Further Reading