-
-
Notifications
You must be signed in to change notification settings - Fork 1
6. Rust
AsynchronousAI edited this page Sep 1, 2025
·
2 revisions
#![no_std]
#![no_main]
extern "C" {
fn printf(fmt: *const u8, ...) -> i32;
}
fn square(num: i32) -> i32 {
num * num
}
#[no_mangle]
pub extern "C" fn main() -> i32 {
let fmt: *const u8 = b"The square of %d is: %d\0".as_ptr(); /* convert to a pointer for the null terminated string, thats how it expects in C */
let n: i32 = 10;
unsafe {
printf(fmt, n, square(n));
}
return 0;
}For this example we will not be using the standard libraries with #![no_std].
extern "C" is used to access printf. The string has to be converted into a pointer for a null terminated string, but you could always write a custom function in Luau to avoid this.
I compiled the code in Compiler Explorer with options of: --target=riscv32imfd-unknown-none-elf, you can also pick to use -C opt-level=3 for optimizations.
From the ASM file, you can generate Luau like usual:
reasm main.s -o main.luauImportant
The compiler will be assuming the entry point to be main, if you would like another entry point symbol you can use the -e symbol parameter.