Skip to content

Instantly share code, notes, and snippets.

@anirudhb
Created April 20, 2019 04:45
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anirudhb/c1e72a838b76d946d6879e54a80bc6fa to your computer and use it in GitHub Desktop.
Save anirudhb/c1e72a838b76d946d6879e54a80bc6fa to your computer and use it in GitHub Desktop.
Rust 1.34 Tiny hello world, comparable to C version
// Tiny "Hello, World!" program in Rust.
#![feature(no_core, lang_items)]
#![no_std]
#![no_core]
#![no_main]
#[lang = "panic_info"]
struct PanicInfo {}
#[panic_handler]
fn panic(info: &PanicInfo) -> ! { loop {} }
#[lang = "eh_personality"]
extern fn eh_personality() {}
#[lang = "sized"]
trait Sized {}
#[lang = "copy"]
trait Copy {}
#[lang = "freeze"]
unsafe trait Freeze {}
#[link(name = "c")]
extern "C" {
fn write(fd: i32, buf: *const i8, count: usize) -> isize;
fn exit(status: i32) -> !;
}
#[no_mangle]
pub unsafe extern "C" fn main() -> ! {
let s = b"Hello, World!\n";
write(1, s as *const u8 as *const i8, 14);
exit(0);
}
@weiby3
Copy link

weiby3 commented Apr 24, 2019

For x86_64-pc-windows-gnu in windows, line 20 #[link(name = "c")] should be deleted to compile correctly.

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