Can Rust compile to C and C++

There are some languages such as Zig, Nim and Kit that can compile down to C and C++ (I don't think Kit can do this at this time). Can Rust also do the same thing?

Not that I know of. There may be some experimental/toy projects out there, but nothing production ready.

You can go in the opposite direction with c2rust, though.

There's https://github.com/thepowersgang/mrustc which has a pretty specific scope.

Interesting, thanks man :slight_smile: With this project, is it able to trnaslate C++ code as well or do I need to use some C wrapper or soemthing?

Interesting, so if I were to use the embedded-hal crate for Arduino programming, am I able to translate it to C++ as Arduino is programmed in C++?

No idea. I suggest you check out that project yourself if you're interested.

Ok thanks.

How about this approach:

  1. Use rustc to compile your program to the RISC V target. (Use RISC V because it is so simple)
  2. Implement a RISC V instruction to C function generator.
  3. Convert the RISC V binary produced in 1) to C using the function generator created in 2)

Sound crazy? Does to me too.

But gamozolabs on YouTube does exactly that in a couple of 9 hour long live coding sessions on YouTube: Fuzz Week Day 3/4: RISC-V JIT for Extra Perf (Part 1/2) https://www.youtube.com/watch?v=ZAP3J86VUWI

He basically builds a RISC V virtual machine with JIT that emits C on the way to host machine instructions. Everything gets in lined and optimized so there is a good turn of speed. All coded in Rust.

4 Likes

I need to target the AVR processor, not RISC-V

But there is already a Rust target for AVR:

6 Likes

Try it and find out?

1 Like

The question is why you need to translate Rust to C and C++?

3 Likes

Unfortunately Rust generates inferior binary to the AVR processor compared to C++ and I want to avoid using C++ for the Arduino.

It is probably simpler to just improve the LLVM backend for AVR to the point where it generates good enough code for your use case. Compiling to another source language and then optimizing the result again will probably end up with more bugs and performance pitfalls.

4 Likes

I hope they can do it because I honestly have no idea :frowning:

What compiler are you using to compile C/++ to AVR? It's it's clang, you can expect same quality of binaries from the Rust, since they share the compiler middle/back end.

If not, here's some possible reasons:

  1. The clang you're using and the avr-rust uses different LLVM version, or uses different set of optimization paths. In this case you can let rustc to generate LLVM IR instead and let your clang toolchain compile this IR.

  2. Your Rust code is not well optimized compared with your C/++ code. In this case, you can optimize your Rust code more to get better binary :slight_smile:

1 Like

It's got to be AVR-GCC, since that's what Arduino ships with and almost everyone has used since forever

2 Likes

There’s always assembly. It’s been over a decade since I did any AVR programming, but as I recall they had a pretty friendly instruction set and good manuals. I was generally using the ones that didn’t have enough RAM for a proper stack, though.

Too hard to use mate.

Interesting.

That's actually pretty clever, though. :slight_smile:

What makes it inferior? Binary size? Speed or something like that?

2 Likes