An example of “Hello World” with raw Rust:
#![feature(no_core,lang_items,start)]
#![no_core]
#![no_std]
#![no_main]
#[lang = "sized"]
pub trait Sized {}
#[lang = "copy"]
pub trait Copy {}
#[no_mangle]
#[start]
pub extern fn _start ()
{
loop {}
}
You can compile it I think with:
rustc +nightly --crate-type bin -C link-arg=-nostartfiles test.rs
In such book you could obviously use core and std but this is as minimal as it can get (almost) ;-)
@jarkko I would beg to differ. Understanding the language shouldn't be disrupted by having to figure out, how to turn it into machine code. Once we go beyond simple single file examples, just using rustc is not enough and burdening the user with the rather archaic Makefile syntax and details of compiling and linking would just distract from the topic. I would even argue, that single file examples benefit from the ability to do "cargo run".
@jarkko Even though my experience with Rustlang is minimal, I can 100% agree with this. Overall the usage of Cargo in documentation examples gives me NPM vibes even though Rustlang should appeal to those working with low-level systems (C, Cpp).
I really hope gcc-rs and the standardization process changes how these books introduce developers to Rustlang.
@jarkko well, we might be roughly the same age (although my journey did start with QBasic before moving on to Turbo C++). I'm not complaining about modern Dev tools. On the contrary. I just think that focus on Rust as a language isn't helped by using rustc and Makefiles instead of doing a simple "cargo run".
@jarkko I have bounced off rust several times because cargo is bloated unusable npm-rot. I see glimpses of a decent language under there, but it is basically undocumented as everything assumes you want the webdev experience.
@jarkko
Some people came to me to know how to learn programming and I told them to starts with logic, algorithmic and data structures then choose a programming language that resonates with them. I don't understand why people want to learn using to code by directly learning a language from books like the one you're describing.
What they actually end up learning is not how to program but just how to use a toolchain/framework ; this knowledge is perishable and isn't portable and leads to frustration.
@jarkko you're probably trolling but I'll bite:
Most of the time "Linux programming" implies some kind of libc because it's explicitly part of the operating system you're programming for (vs 'just the kernel', that's what the GNU+Linux meme is about). This doesn't make your program "less Rust".
If you rather talk to the kernel directly you would use the x86_64-unknown-none target. There's a blogpost I wrote in 2023 on how to do this step by step: https://vulns.xyz/2023/03/linux-executable-from-scratch-with-x86_64-unknown-none-rust/
@jarkko Spoiler: I do use cargo in this blogpost.
Turns out having a reasonable build system doesn't stop one from in-depth exploring the Linux binary/assembly interface.
Also the code works on stable Rust, no nightly necessary. ✨
@jarkko The "raw Rust" code you posted is lowkey kinda ass and you're (most likely) trying to butcher `x86_64-unknown-linux-gnu` into being libc-free (which explicitly targets glibc).
Check the output of `rustc --print cfg` vs `rustc --target x86_64-unknown-none --print cfg`.
@jarkko ah. I had the feeling that you had a special use case (ok, maybe not that special for a systems language). I could imagine, that this use case is niche enough, that nobody bothers to write books for it.