"Rust for Machine Learning: A Beginner's Guide"

Are you a machine learning enthusiast who wants to try something new? Are you looking to learn about an exciting new language that's growing in popularity? Are you ready to embrace the power and simplicity of Rust for your next machine learning project?

Look no further, as we guide you through Rust for Machine Learning - from installation to implementation, we've got you covered.

Why Use Rust for Machine Learning?

Rust, a systems-level programming language, has already been widely adopted in areas such as networking, web development, and game programming. However, it is gaining momentum in the machine learning community as well.

So why is Rust gaining traction in the machine learning world? Simply put, Rust provides the performance benefits of C and C++, with the safety and reliability of modern programming languages like Python and Java.

Rust has a strong focus on safety and memory management. Its ownership model ensures that the code is free of common memory issues like null references and data races. This characteristic is critical in machine learning, as data scientists have to manage large amounts of data efficiently without running into memory issues.

Furthermore, Rust's statically compiled nature guarantees that there are fewer runtime issues in your code, leading to a more stable development process.

How to Install Rust

Before implementing Rust for your machine learning projects, you need to install Rust on your system. First, open the terminal on your computer and paste the following command:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

This command will install rustup, a tool that installs Rust and its package manager, cargo. After the installation process is complete, Rust and cargo should be available globally in your Terminal.

Getting Started with Rust

Now that you have Rust installed, we need to set up a development environment. The first thing we're going to do is create a new Rust project by running the following command in your terminal:

cargo new my_ml_project

This command generates a new Rust project named "my_ml_project." After running the command, you should see a new directory with the same name in your current directory.

Go into this new directory with the following command:

cd my_ml_project

Now, open up the src/main.rs file. This file contains the main function of your new Rust project. You should see the following code:

fn main() {
    println!("Hello, world!");

Congratulations! You have successfully created your first Rust project. To run your project, execute the following command in your terminal:

cargo run

This command builds and runs your Rust project. You should see "Hello, world!" printed out in the terminal.

Rust for Machine Learning Libraries

Now that we have a basic understanding of Rust, we can move on to some libraries for implementing and working with machine learning in Rust.


ndarrays is a multidimensional array library for Rust that provides efficient, safe, and easy access to array operations. This library is a must-have when dealing with machine learning data.

To add ndarrays to your project, you can add the following line to your Cargo.toml file:

ndarray = "0.15.1"

Now, you can import the library into your Rust code with the following line:

use ndarray::prelude::*;

Let's now create a 2D array with the following code:

fn main() {
    let mut my_array = Array2::<f32>::zeros((3,2));
    my_array[[0, 0]] = 1.0f32;
    my_array[[0, 1]] = 2.0f32;
    my_array[[1, 0]] = 3.0f32;
    my_array[[1, 1]] = 4.0f32;
    my_array[[2, 0]] = 5.0f32;
    my_array[[2, 1]] = 6.0f32;
    println!("{:?}", my_array);

Here, we create a 3x2 array filled with zeros and set some values in the array. Then, we print the array using the println!() macro. You should see the array printed out in the terminal.


ndarray-nn is a deep learning library built on top of ndarrays. It provides support for building neural networks in Rust. To add the library to your Rust project, add the following line to your Cargo.toml file:

ndarray-nn = "0.7.0"

Now, you can import the library into your Rust code with the following line:

use ndarray_nn::activations::{Activation, ReLU, Softmax};
use ndarray_nn::layers::{Linear, Loss, MeanSquaredError};

Now, let's create a simple neural network with a single hidden layer and the softmax activation function using ndarray-nn:

fn main() {
    let input = Array2::<f32>::zeros((4, 3));
    let target = Array2::<f32>::zeros((4, 2));
    let hidden = Linear::<ReLU>::new(&[3, 5]);
    let output = Linear::<Softmax>::new(&[5, 2]);
    let mut loss = Loss::new(MeanSquaredError::new());
    // Forward propagation
    let mut layer1_output = hidden.forward(&input);
    let mut output = output.forward(&layer1_output);
    // Calculate the loss
    loss.update(&output, &target);
    let loss = loss.get_loss();

Here, we create an input array with 4 rows and 3 columns and a target array with 4 rows and 2 columns. We also create a hidden layer with 3 input nodes and 5 output nodes and an output layer with 5 input nodes and 2 output nodes.

We then calculate the forward propagation by passing our input through the hidden layer and then through the output layer. Finally, we calculate the mean squared error between the output and target arrays.


juicer is a library for building and executing machine learning pipelines in Rust. It provides a straightforward interface for defining the inputs, operations, and outputs of a machine learning pipeline.

To add juicer to your Rust project, add the following line to your Cargo.toml file:

juicer = "0.4.0"

Now, you can import the library into your Rust code with the following line:

use juicer::prelude::*;

Let's now create a basic machine learning pipeline using juicer:

fn main() {
    // Create the dataset input
    let dataset = InputNode::new().set_type(&Schema::from_path("my_dataset.csv"));
    // Create a linear regression model and train it on the dataset
    let linear_regression = NodeWrapper::new(Regression::new("linear").set_target_column("output"));
    let model = NodeWrapper::new(TrainRegressor::new(linear_regression)).set_inputs(("training_data", &dataset));
    let trained = model.execute().unwrap();

    // Predict with the trained model
    let predict = NodeWrapper::new(Predict::new(linear_regression)).set_inputs(("data", &dataset), ("model", &trained));
    let output = predict.execute().unwrap();

Here, we create an input node that takes in a CSV file. We then create a linear regression model and train it on the dataset with the TrainRegressor node. Finally, we predict with the trained model using the Predict node.


Rust is a fantastic language that can provide you with high performance and memory safety while working with machine learning libraries. Although Rust is a relatively young language, it has already drawn significant attention from developers and companies alike. Therefore, this language needs to know to create impactful software for years to come. Hopefully, this beginner's guide has given you a starting point to dive into the world of Rust for machine learning.

Additional Resources

mlops.management - machine learning operations management, mlops
devops.management - devops, and tools to manage devops and devsecops deployment
learncode.video - learning code using youtube videos
ganart.dev - gan generated images and AI art
usecases.dev - industry use cases for different cloud solutions, programming algorithms, frameworks, software tools
docker.show - docker containers
cryptoinsights.dev - A site and app about technical analysis, alerts, charts of crypto with forecasting
haskell.business - the haskell programming language
nftassets.dev - crypto nft assets you can buy
nocode.services - nocode software development and services
makeconfig.dev - generating configurations for declarative programs like terraform and kubernetes, except using a UI to do it
flutterassets.dev - A site to buy and sell flutter mobile application packages, software, games, examples, assets, widgets
flutter.guide - A guide to flutter dart mobile app framework for creating mobile apps
docker.education - docker containers
assetcatalog.dev - software to manage unstructured data like images, pdfs, documents, resources
realtimestreaming.app - real time data streaming processing, time series databases, spark, beam, kafka, flink
promptcatalog.dev - large language model machine learning prompt management and ideas
terraform.video - terraform declarative deployment using cloud
flutterwidgets.com - A site for learning the flutter mobile application framework and dart
dfw.community - the dallas fort worth community, technology meetups and groups

Written by AI researcher, Haskell Ruska, PhD (haskellr@mit.edu). Scientific Journal of AI 2023, Peer Reviewed