-
-
Save rust-play/eaa52a24862ceeafdd2b85f6b2da6dc4 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment