Machine Learning in Rust; Is it worth learning it?
Tags: rust, machinelearning, beginners, tutorial
Published: 2023-05-22 14:05:43

As of 2023 what does the Rust language offer for ML and AI? How does it compare to the Giants ( e.g. Pytorch, Tensorflow libraries )? Most importantly, does it help you towards becoming a professional Ml engineer? These are the questions I had as a newcomer to the ML field and a programming language enthusiast. I tried to scratch the surface and report my findings so you can also have an idea of ML outside of Python, C++.


![Rust, SmartCore, Linfa, Tensorflow, Pytorch.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9nbhou6favo0aiwlfg9j.png)

---
---

## Is it hard?
Yes, especially if you come from [dynamically-typed](https://www.educative.io/answers/what-is-dynamic-typing) languages ( Python, Javascript ). Rust is a VERY strict/statically typed language, who benefits from this? Well actually Rust converts a lot of bugs of dynamically languages to actual errors, but do not have any high expectations for the first time. Rust also has many features high-level don't have: macros, ownership/borrowing, traits/implementations, Results ( error handling ) and probably other. Learning Rust is a challenge by itself, but if you have the balls, then is it worth it?


![Official Unofficial Rust Learning Curve](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3lv8qb9erquyhwv68aiz.png)

---
---

## Is it worth it?
Yep, absolutely. You will learn not only AI by creating them from the ground up but also low-level programming, a skill not many master or even know. There is no Keras or torch.NN api that has all the layers you will never probably use, there is Linfa but is compares more to Scikit learn that Keras or torch.NN. If you want a Conv2d layer then you will have to build it your own. It is a feat to build a Deep Convolutional Neural Network in Rust that you will put in your Résumé. I will give you a pause to decide whether you want to embark on such journey and next I will talk about what APIs are available in Rust and if they are worth learning.

---

## ML/AI APIs in RUST
In short here is a list of the some useful ML/AI crates in Rust
- [Linfa](https://github.com/rust-ml/linfa)
- [smartcore](https://crates.io/crates/smartcore)
- [ndarray](https://crates.io/crates/ndarray)
- [petgraph](https://crates.io/crates/petgraph)
- [ndarray_image](https://crates.io/crates/ndarray-image)
- [cv](https://github.com/rust-cv/cv)
- [rust-gpu](https://github.com/EmbarkStudios/rust-gpu)

The Linfa and Smartcore crates are both similar to scikit-learn, ndarray provides multi-dimensional vectors ( tensors ), petgraph provides an API for graphs,ndarry_image helps you read images and convert them to RGB 2d arrays and rust-gpu allows you to use the gpu but I don't know if it is compatible for ML and the other crates. There are probably other libraries as well, but I didn't find any crate for Natural Language Processing ( NLP ) or Audio Processing. Please recommend any other crates if you find them helpful. Now the most important question is, what can you actually build without developing a Tensorflow for Rust library?

---

## What can you create in a descent amount of time?
Let's start with what you will not create:
- NLP AIs, don't expect to build an Alexa or Siri in Rust, the - technology still isn't there.
- Audio Processing, no fake voices.
- GANs, not these kind of guns you are thinking but Generative Adversarial Networks ( e.g. midjourney, dall-e )

Ok, now that you have some blurry expectations here is what you can probably create:
- Regression models ( linfa, SmartCore )
- Computer Vision Models ( cv )
- Nearest Neighbor algorithms ( linfa, SmartCore )
- Classification Models, there is no goo API you will have to create an API yourself ( your skills )
- Supervised Models ( linfa, SmartCore )

There are some resources that you can follow but they intend to get your feet wet and not much else like this [Youtube video](https://www.youtube.com/watch?v=FI-8L-hobDY) that builds a neural network from scratch in Rust.

---

## GPU support
Good luck :).

---

## Learn ML in C++, it would be easier…
Learn C++ which is easier than Rust as it follows paradigms from popular languages and doesn't have many complex concepts, if you happen to not know every popular python ML library uses C++ in the back end which means that they have an API in C++. Tensorflow for example has [Tensorflow Lite](https://www.tensorflow.org/lite/microcontrollers/library), [pytorch also supports an API in c++](https://pytorch.org/cppdocs/frontend.html) and [opencv](https://docs.opencv.org/4.x/df/d65/tutorial_table_of_content_introduction.html) was built in C++. Moreover C++ is faster and will speed up your workflow.

---

## Looking for something easier?
If you want to learn more about ML and you happen to know a little bit Web development I made a [blog](https://dev.to/yaksirhc/reactjs-ai-how-to-combine-ai-with-react-without-any-machine-learning-experience-5c76) on how you can integrate Tensorflow with React.js. If you are just starting out then learn [Pytorch](https://www.youtube.com/watch?v=V_xro1bcAuA) be careful to not get stuck in tutorial hell though.