Development

How to Use Rust without Standard Library

Captain Salem 2 min read

How to Use Rust without Standard Library

One of the critical features of the Rust programming language is its standard library, which provides a rich set of functionality for everyday tasks such as file I/O, networking, and data manipulation.

However, there may be situations where we want to use Rust without the standard library, either to reduce the output binary size or to avoid unnecessary features in our project.

This tutorial will explore how to use Rust without the standard library.

Setup Your Rust Project.

The first step is to configure your Rust project. We can do this simply by calling the cargo command as:

$ cargo **new** --bin <project_name>

The command will create the Rust project with a directory as specified in the <project_name>. The `--bin flag tells Rust that we wish to build the project as binary rather than a library.

Remove Rust Standard Library

By default, any Rust project will use the standard library. To use Rust without the standard library, we need to configure your project to use no_std.

To do this, edit your project’s Cargo.toml file. Add the following lines to the [dependencies] section:

**[dependencies]**
 core = "1.6.0"
 compiler_builtins = "0.1.91"

The above tells Cargo to include the core and compiler_builtins crates in your project. The core crate is a subset of the standard library that provides essential types and functions, while the compiler_builtins crate provides low-level functions that the Rust compiler needs.

The Rust Core Library is the dependency-free foundation of The Rust Standard Library. It is the portable glue between the language and its libraries, defining the intrinsic and primitive building blocks of all Rust code. It links to no upstream libraries, no system libraries, and no libc.

Source: Rust Docs

Next, create a new file called src/lib.rs. This file will contain the Rust code for your project.

In this file, add the following line at the top:

#![no_std]

This tells the Rust compiler to compile the project without the standard library.

The following example is a simple project demonstrating how we can build a project without Rust’s standard library.

#![no_std]
 
 #[panic_handler]
 **fn** **panic**(_info: &core::panic::PanicInfo) -> ! {
   **loop** {}
 }
 
 **fn** **add**(a: u32, b: u32) -> u32 {
   a + b
 }

The function in the code above adds two unsigned 32-bit integers and returns the result. Note that the function does not use any types or functions from the standard library.

The #[panic_handler] attribute defines a panic handler function, which is called if the program encounters a runtime error. In this case, the function enters an infinite loop, a common way to handle panics in embedded systems.

We can then define the main function as:

fn main() {
   let result = add(2, 3);
   assert_eq!(result, 5);
 }

To test the project, we can build it and run it on the target system to verify.

Conclusion

In this tutorial, we quickly explored how we can configure a Rust project without using the Rust’ standard library. Using Rust without the standard library can be a useful technique for reducing the size of the binary or for working with systems that do not support the standard library. However, it requires a deeper understanding of the language and its underlying features, and may not be appropriate for typical use case.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.