Skip to content

RUST for AI software development

What is RUST?

Rust is a modern systems programming language focused on three core goals: performance, memory safety, and concurrency.

Think of it as having the raw speed and low-level control of languages like C or C++, but with a powerful “guardian” built-in—the borrow checker. This feature checks your code at compile time to prevent entire classes of common bugs (like null pointer dereferences and data races) that plague other systems languages.

Key Features:

  • Performance: It compiles to efficient machine code and gives you fine-grained control over system resources, making it ideal for performance-critical tasks.
  • Memory Safety (Without a Garbage Collector): Unlike languages like Python or Java that use a “garbage collector” (which can cause unpredictable pauses), Rust manages memory at compile time. This results in highly efficient and predictable performance.
  • Concurrency: Rust’s safety rules make it much easier and safer to write programs that do multiple things at once without them interfering with each other.
  • Modern Tooling: It comes with a best-in-class package manager and build tool called Cargo, which makes managing dependencies and building projects simple.

Is It Useful for AI Software Development?

Yes, it is very useful and its role is rapidly growing, but it’s not used in the same way as Python.

To understand its usefulness, it’s best to compare it to Python, which is the dominant language in AI.

👑 The “Python for Prototyping” World

Most AI models, especially in research and experimentation, are built using Python. This is not because Python is fast (it’s actually quite slow) but because it’s an excellent “glue language.”

  • AI researchers use Python to write simple code that calls high-performance, pre-compiled machine learning frameworks that are often written in C++.
  • These libraries do the actual “heavy lifting” (like massive matrix calculations) on the GPU.
  • Python’s easy syntax is perfect for rapid prototyping and iterating on new ideas.

🚀 The “Rust for Production” World

Rust is not trying to replace Python for research. Instead, it is becoming the “go-to” language for the high-performance, production-grade parts of the AI pipeline.

Here are the specific areas where Rust shines in AI:

  1. High-Performance Backends: Rust is increasingly used to write the fast libraries that Python calls. Developers are replacing C++ components with Rust to get the same speed but with much greater safety and fewer bugs.
  2. Data Engineering: The data-processing step (cleaning and preparing massive datasets) is often a bottleneck. Rust-based tools are proving to be dramatically faster than their Python equivalents for data manipulation.
  3. Inference and Edge AI: This is where Rust is a clear winner. When you want to deploy a trained model (known as “inference”)—especially on resource-constrained devices like a smartphone, a car, or an embedded system—Rust is a perfect fit. Its small binary size, low memory footprint, and lack of a garbage collector mean it runs efficiently anywhere.
  4. AI Tooling: Many of the new, ultra-fast tools that support the AI ecosystem are being built in Rust, including code editors, linters, and package managers.

In summary: You can think of the AI world as a two-part system:

  • Python (The Researcher): The “brain” for experimenting, prototyping, and orchestrating tasks.
  • Rust (The Engine): The high-performance, safe, and reliable “engine” that executes the most demanding tasks with maximum speed.

🛠️ How to Install Rust

The official and recommended way to install Rust is by using rustup, the Rust toolchain installer and version manager. It bundles everything you need, including:

  • rustc: The Rust compiler.
  • cargo: The Rust build tool and package manager.
  • rustup: The toolchain manager itself, for updating Rust.

For macOS, Linux, or WSL (Windows Subsystem for Linux)

  1. Open your terminal.
  2. Run the following command and follow the on-screen instructions. It will download a script and start the installation.
    Bash
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    you may need sudo rights to install the software. You can do that with `sudo su´.
  3. Once it’s done, you may need to restart your terminal or run source $HOME/.cargo/env to add Rust’s tool directory to your system PATH.

For Windows

  1. Go to the official Rust installation page: rust-lang.org/tools/install
  2. Download and run the rustup-init.exe installer.
  3. It will guide you through the installation. It will also prompt you to install the Microsoft C++ build tools, which are required for Rust to work.

Verify Your Installation

After installation, open a new terminal and run:

Bash

rustc --version
cargo --version

You should see the version numbers for the Rust compiler and Cargo, which means you’re all set!
If not, try running source $HOME/.cargo/env by exiting the sudo mode.
On Mac book, if next time you go to the terminal and hit “cargo –version” or run code and it could not find cargo, then quit, reopen the terminal and run source $HOME/.cargo/env again.


1. “Hello, World!” Project (The cargo Way)

The best way to create a new Rust project is by using cargo.

Step 1: Create a new project

In your terminal, run:

Bash

cargo new hello_world

This command creates a new directory called hello_world with the following structure:

hello_world/
├── Cargo.toml
└── src/
    └── main.rs
  • Cargo.toml: The “manifest” file. It contains project settings and lists your project’s dependencies (called “crates”).
  • src/main.rs: This is where all your application code goes.

Step 2: Look at the code

If you open src/main.rs, you’ll see cargo has already generated a “Hello, world!” program for you:

Rust

fn main() {
    println!("Hello, world!");
}
  • fn main(): This is the main function, the entry point of every executable Rust program.
  • println!("Hello, world!");: This line prints text to the console. println! is a Rust macro (indicated by the !), which is a way of writing code that writes other code.

Step 3: Run the project

Navigate into your new project directory and run:

Bash

cd hello_world
cargo run

cargo run will first compile your project and then execute the resulting program. You should see this output:

   Compiling hello_world v0.1.0 (/path/to/hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 0.50s
     Running `target/debug/hello_world`
Hello, world!

2. Basic Code Examples

Here is a single example file (src/main.rs) that shows variables and functions. In Mac, for example, you can open it with nano src/main.rs.

Rust

// This is the main function, the entry point of the program.
fn main() {
    // 1. VARIABLES
    // By default, variables are immutable (they can't be changed).
    let an_immutable_number = 5;
    println!("The immutable number is: {}", an_immutable_number);

    // To make a variable changeable, you MUST use the `mut` keyword.
    let mut a_mutable_number = 10;
    println!("The mutable number starts at: {}", a_mutable_number);

    a_mutable_number = 20;
    println!("The mutable number is now: {}", a_mutable_number);


    // 2. CALLING A FUNCTION
    // We can call our custom function `add_numbers`.
    let sum = add_numbers(an_immutable_number, a_mutable_number);
    println!("{} + {} = {}", an_immutable_number, a_mutable_number, sum);
}

// This is a custom function.
// It takes two 32-bit signed integers (i32) as parameters.
// The `-> i32` part declares that this function returns a 32-bit signed integer.
fn add_numbers(x: i32, y: i32) -> i32 {
    // In Rust, if the last line of a function is an expression 
    // (with no semicolon), it is automatically returned.
    x + y 
}

If you replace the code in your src/main.rs file with the code above and run cargo run, you will see:

The immutable number is: 5
The mutable number starts at: 10
The mutable number is now: 20
5 + 20 = 25

The best place to continue learning is the official, and excellent, documentation known as “The Rust Book”:
The Rust Programming Language

Rust by Example

Leave a Reply

error: Content is protected !!