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
There's https://github.com/thepowersgang/mrustc which has a pretty specific scope.
Interesting, thanks man 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.
How about this approach:
- Use rustc to compile your program to the RISC V target. (Use RISC V because it is so simple)
- Implement a RISC V instruction to C function generator.
- 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.
I need to target the AVR processor, not RISC-V
But there is already a Rust target for AVR:
Try it and find out?
The question is why you need to translate Rust to C and C++?
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.
I hope they can do it because I honestly have no idea
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:
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.
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
It's got to be AVR-GCC, since that's what Arduino ships with and almost everyone has used since forever
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.
That's actually pretty clever, though.
What makes it inferior? Binary size? Speed or something like that?