Skip to content

Instantly share code, notes, and snippets.

@RandyMcMillan
Forked from rust-play/playground.rs
Last active December 2, 2025 16:35
Show Gist options
  • Select an option

  • Save RandyMcMillan/79aaf0db4bbb7d5959a71d7ae1fdcacd to your computer and use it in GitHub Desktop.

Select an option

Save RandyMcMillan/79aaf0db4bbb7d5959a71d7ae1fdcacd to your computer and use it in GitHub Desktop.
minimal_crust.rs
// main.rs - A minimal Crust program with tests
//
#![cfg_attr(not(test), no_std)]
#![cfg_attr(not(test), no_main)]
extern crate libc;
#[cfg(not(test))]
use core::panic::PanicInfo;
#[cfg(not(test))]
//#[panic_handler]
#[allow(unused)]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}
#[cfg(not(test))]
#[unsafe(no_mangle)]
pub extern "C" fn main() -> i32 {
let s = b"Hello world!\n\0";
unsafe {
libc::printf(s.as_ptr() as *const i8);
}
0
}
#[allow(unused)]
fn add(x: u32, y: u32) -> u32 {
let r = x + y;
let s = "%d + %d = %d\n\0";
unsafe {
libc::printf(s.as_ptr() as *const i8, x, y, r);
}
r
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
#[allow(unused_imports)]
use anstream::println;
#[allow(unused)]
// build.rs - Required for building Crust programs (On Mac OS at least)
fn build_rs() {
// Link against the system C library on macOS
//println!("cargo:rustc-link-lib=System");
}
// Cargo.toml
@RandyMcMillan
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment