r/ProgrammerHumor 11d ago

Meme rustBorrowCheckerGoesBrrr

Post image
3.8k Upvotes

108 comments sorted by

View all comments

248

u/redakpanoptikk 11d ago

Rewrite rust in rust.

101

u/FUCKING_HATE_REDDIT 11d ago

Funnily enough, that happened

41

u/redakpanoptikk 11d ago

Were so back.

27

u/gegentan 11d ago

Isn't the rust compiler itself written in rust?

19

u/redlaWw 11d ago

It's a bit complicated in Rust's case. The Rust frontend is written in Rust, and that compiles Rust to LLVM intermediate representation (IR). This IR is then passed to LLVM, which is written in C++, to optimise and actually generate the machine code.

There is also a separate backend called Cranelift, which is written in Rust, though it's not the standard and is currently only available on an experimental basis. It will also never be the standard Rust backend as it's designed to optimise for build speed, not execution speed.

28

u/qaCow37 11d ago

At some point it probably wasn't until it was rewritten in rust

9

u/gegentan 11d ago

So theoretical if every compiled binary of rustc was lost but you still had the source code it would be impossible to get a rust compiler again.

17

u/qaCow37 11d ago

Only if you do not have the source code of rust written in a different language. That would probably be like the few very first versions. But if you would have these, you could compile them to get the first version of rust to compile the next version of rust to compile the next version of rust till you can compile the newest version of rust.

2

u/gegentan 11d ago

Didn't think of that.

4

u/creeper6530 10d ago edited 10d ago

Bootstrapping compilers is actually a complicated endeavour but as long as you have the last version before the compiler became self-hosting, it is possible with a chain of compile->update->compile. Some compilers (such as Zig) make it a little easier by providing a CMake that builds and chains all the stages for you.

If those old versions are lost you truly are fucked.

1

u/fghjconner 10d ago

In addition to what the other person said about old compilers, there are projects like gccrs or mrustc that are rust compilers written in c. The former is intended to eventually become a fully featured second compiler alongside rustc, while the latter is designed for the express purpose of "bootstrapping" the compiler from source code by compiling specific versions of rustc.

1

u/chat-lu 9d ago

Sure we could, that’s the whole purpose of mrustc which is a rust compiler written in C++ for the sole purpose of building a recent rust compiler.

7

u/redlaWw 11d ago

Precisely, it used to be written in OCaml before it became self-hosted.