39 points · by unripe_syntax · 1y ago · 53 comments
Can rust generate usefull machine code without a runtime and having to break one leg like C?
Can I do the same in rust?
Afaik, the only runtime parts of Rust are panic handling, standard library and thread management.
You can disable the standard library with `#![no_std]`, and I'm fairly sure you can also disable the panic handling (but not 100%) and I don't know if you can disable thread management at all. But if you can, I think you can have what you're asking for :) Edit: In fact, the `no_std` might actually disable all of the above, not just the stdlib.
Note that the "runtime parts" in question here refers to initializing OS-level threads, the same as in C. It's not referring to any sort of userspace green thread runtime (which you would need to bring in yourself).
no_std disables the parts of the standard lib that rely on having an OS (e.g. threads) or having an allocator. (You can get the allocator parts back in a no_std program if you define your own allocator.)
"Panics" aren't something you'd disable, a panic is just a mechanism for crashing the program in a controlled manner. Rust lets you decide whether or not you want panics to abort the program immediately or whether to unwind the program and run destructors, and you can configure this in any Rust codebase via a config key/compiler flag. (If you're using no_std then you can technically also define panics as being an infinite loop, rather than crashing.)
It has the exact same amount of runtime requirements as C. The default configurations aren’t optimizing for size, but if you want to strip it down, you can.
By the way, even C has a runtime, without which several ISO C features aren't available.
That is why there is a freestanding section.
For example, Microsoft has thrown away all their C code on Pluton CPU firmware and replaced it with Rust.
Likewise Google has been shipping Rust on Linux kernel downstream fork on Android for several versions now, regardless of upstream drama.
And any recent Windows 11 version runs Rust kernel code, every single time anything on the screen, after the GDI regions handling C kernel code has been rewriten in Rust.
The cargo cult that C is the be all, end all of systems programming gets tiresome after a few decades.
Rust's global allocator is easy enough to replace on stable: https://doc.rust-lang.org/std/alloc/trait.GlobalAlloc.html
Per standard container allocators ala C++'s std::allocator haven't been stabilized, but are available on nightly: https://doc.rust-lang.org/std/alloc/trait.Allocator.html
Alternatively, you can roll your own allocator ecosystem easily enough - my limited foray into this is mostly focused on interop with specific 3rd party allocator APIs from C, C++, Win32, etc.: https://docs.rs/ialloc/ , there are plenty of crates focusing on arena allocation etc. instead.
> Can rust generate usefull machine code without a runtime and having to break one leg like C?
Yes.
At one extreme, you can create #![no_std] crates ( https://docs.rust-embedded.org/book/intro/no-std.html ) which don't assume you can create threads, write to stdio, or even allocate heap memory. I've gotten Rust static libraries linking into "unsupported" platforms in minutes (when it had an LLVM-compatible linker available.)
At the other extreme, I've previously ported Rust's standard library to an unsupported NDAed platform. It's easy to stub out unsupported bits - the code for such stubs already exists to support wasm32-unknown-unknown (arguably a mistake, but it's a useful one!) - and replacing those stubs with real code isn't too onerous in my experience either.
Middle grounds of `extern crate alloc;` let you support subsets of "the standard library" as well.
Yanking all "runtime" support out of existing platforms might be a bit trickier (e.g. there's a little code to support populating std::env on *nix, a little mucking with default signal handling, and e.g. windows builds will use Win32 APIs for backtracing and unwinding for panics - ripping that all out is more obnoxious, but also pointless IMO.)
Some non-NDAed examples of targeting funky stuff:
• https://github.com/MaulingMonkey/uefi-hello-world (why write hello world for windows when you can directly boot hello world?)
• https://github.com/MaulingMonkey/rust-opendingux-test (old, linuxy - see Notes.md, consider using -Zbuild-std instead of Xargo these days?)
All the core decisions are the same as in C++:
- RAII
- Lock-based concurrency
- In-place mutation
People get confused when they hear that Rust is 'related to ML' and has Refs, but Rust Refs have nothing to do with ML Refs: ML Refs are mutable pointers to immutable data, Rust Refs are pointers to data mutated in-place.
Reference counting is not 'Rust memory management' it's a well-known technique with well-known problems, and Rust doesn't bring anything new to the table here.
Edit: RIAA -> RAII
While this may work as a rough heuristic, it's not true in theory nor in practice. First example that comes to mind: Rust has proper sum types (Called enums), which are used everywhere, and C++ doesn't have anything like it.
I'm sure you could technically emulate all this in C++ is you keep throwing template metaprogramming at it, but at that point anything is a C++ subset and the statement loses all pratical meaning.
Jokes aside, I think the biggest difference between Rust and C++ lies in metaprogramming and generics. Rust macros are a trip.
Rust's sum types and traits are also much more deeply integrated into the syntax of the language than std::variant or concepts.
Not at all. In a Rust program, all objects are supposed to be movable via a memcpy (i.e. a bitwise copy) by default. Immovable objects are only supported as an afterthought, via the Pin<> featureset (which is widely regarded as unintuitive, clunky and in need of revision). C++ objects are immovable by default; alternately, they may support custom "move" operators that are closer to the way .take() is used in Rust, i.e. they leave behind the original object in some sort of undetermined state.
These are quite significant differences that make it hard to have consistent interop across both languages.
But what happens to inbound references when I'm moving my object? Who is in charge of updating them?
This is like saying that Julia is a subset of Python with JIT compilation. Rust and C++ have some conceptual similarities, but they are entirely different languages.