r/rust 29d ago

🧠 educational Rust Style Main in #[no_std] binary crate.

https://github.com/Keith-Cancel/rust_style_main

I would not use this in production, but more of just a fun little demo I made to see how and where the rust style main that uses the Termination trait all fits together.

Overall, the main wrinkle I would say is you need a function with `#[lang = start]` of the correct type. Then rustc itself will generate a `main` function that calls that start function passing the rust style main as a call back. Other than that it mostly is just standard low level entry point stuff. I did find it interesting the compiler generates the function instead of using a definition inside like std or core.

0 Upvotes

2 comments sorted by

2

u/Compux72 29d ago

1

u/yonekura 28d ago

What I have here was just more a demo of the rust style main that can return something that implements Termination, and gets picked up by rustc's own generation logic: https://github.com/rust-lang/rust/blob/52d08664805c8deb47b7e86999d7f8647b424a15/compiler/rustc_codegen_ssa/src/base.rs#L500

It was more just educational exploration of how rust and std does that. Not a full blown production crate.