Rust no_std template

There are many in-depth tutorials that explain how to write a #![no_std] binary. This is just a simple, reusable bare-bones template that works for me.

In your src/main.rs:

#![no_std]
#![no_main]

use core::panic::PanicInfo;

#[panic_handler]
fn pa…


This content originally appeared on DEV Community and was authored by ender minyard

There are many in-depth tutorials that explain how to write a #![no_std] binary. This is just a simple, reusable bare-bones template that works for me.

In your src/main.rs:

#![no_std]
#![no_main]

use core::panic::PanicInfo;

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {}
}

#[no_mangle]
pub extern "C" fn _start() -> ! {
    loop {}
}

In your Cargo.toml:

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

Instead of simply running cargo build to generate a binary like usual, you need to jump through some hoops to generate a binary.

If you are compiling this binary for Linux, you can run:

cargo rustc -- -C link-arg=-nostartfiles

For Windows:

cargo rustc -- -C link-args="/ENTRY:_start /SUBSYSTEM:console"

For macOS:

cargo rustc -- -C link-args="-e __start -static -nostartfiles"

All of this is useful if you plan on running the resulting binary on bare metal. If you're compiling Rust to WebAssembly, none of this is necessary - simply not using the std library is enough for that use case.

If you're targeting the WebAssembly System Interface, it's more simple and you can even use Rust's std library for that use case.


This content originally appeared on DEV Community and was authored by ender minyard


Print Share Comment Cite Upload Translate Updates
APA

ender minyard | Sciencx (2021-04-07T01:48:41+00:00) Rust no_std template. Retrieved from https://www.scien.cx/2021/04/07/rust-no_std-template/

MLA
" » Rust no_std template." ender minyard | Sciencx - Wednesday April 7, 2021, https://www.scien.cx/2021/04/07/rust-no_std-template/
HARVARD
ender minyard | Sciencx Wednesday April 7, 2021 » Rust no_std template., viewed ,<https://www.scien.cx/2021/04/07/rust-no_std-template/>
VANCOUVER
ender minyard | Sciencx - » Rust no_std template. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/07/rust-no_std-template/
CHICAGO
" » Rust no_std template." ender minyard | Sciencx - Accessed . https://www.scien.cx/2021/04/07/rust-no_std-template/
IEEE
" » Rust no_std template." ender minyard | Sciencx [Online]. Available: https://www.scien.cx/2021/04/07/rust-no_std-template/. [Accessed: ]
rf:citation
» Rust no_std template | ender minyard | Sciencx | https://www.scien.cx/2021/04/07/rust-no_std-template/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.