Can Rust compile to C and C++

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

I was told on another post that using embedded-hal and if I used Rust to compile it to AVR, it has inferior performance compared to C++ that is the one huge downside.

Well all he said was "I believe the LLVM support for that is still fairly unoptimized" so that doesn't necessarily mean that C++ will be faster. It could be, but maybe do some testing with a benchmark that is as close as possible to your real use-case if possible. Then you can see if it will actually make a difference for you or not.

If you actually want to use Rust for it it would be lame to miss out if it wasn't a problem. :slight_smile:

2 Likes

Yeah true but I also asked how well supported Rust is on the Arduino forum and people didn't recommend it, one claims that it is actually slower compared to C++ for Arduinos (I don't have the link to that post :frowning: )

1 Like

I don't think (gut judgement) the performance difference will be worth compiling rust to c/c++. Especially based on anecdotal evidence. There will be a number of competing factors affecting performance.

If you want to use rust enough to consider compiling to c++ I would encourage writing it in rust and seeing how your program performs for yourself.

Do let us know if the performance does turn out to disappoint you. I'm sure some people here would like to help you improve it. Probably by optimising the rust source code but who knows :slight_smile:

2 Likes

But this is an AVR processor I want to compile to, its pretty weak on the Arduino which would make a huge difference compared to a powerful 64bit processor.

How would I benchmark it though?

Of course :slight_smile:

I don't believe you know that for sure.

I would not be inclined to trust anyone's claims of Rust being slower, or faster, than C/C++ on the PC let alone the AVR chips.

Over my first year of using Rust I have re-implemented an number of little C codes in Rust, partly as a learning exercise, partly to demonstrate to myself I would not be losing out on performance by moving to Rust. These efforts were discussed here when I ran into difficulties and all kind of solutions were presented.

Well, the result of all that discussion was that a lot of proposed solutions were slower than C. By factors of 2 to 4 and more. But some matched the performance of C and even exceeded it. A huge variation for the same algorithm expressed slightly differently, using iterators instead of for loops for example.

At the end of the day then, I would not trust any random comments about this found on the net. Better to just install the avr-gcc and avr-rust and write some code to put through them. You can dump the resulting assembler instructions to see how well they compare. And presumably do some timing on an actual AVR chip.

But do remember to try different approaches in your Rust code, it can make a big difference.

In the worst case, that Rust performance on AVR turns out to be terrible, I would not start messing with Rust to C compilation. There is no assurance it will do any better and it all gets messy. Just use C.

Like anything else. Write some code representative of the things you want to do with the device, in Rust and C/C++. Then run it and time it.

The AVR has pins you can wiggle to flash LEDs and measure frequencies with. What else do you need?

Also use objdump to have a look at the assembler instructions generated in both cases.

4 Likes

Do you know any good ways to do performance tests?

Of course.

I would probably use C++ as this is what the arduino uses.

Implement it. If your embedded code works and meets its time requirements then Rust is fast enough for your use case.

2 Likes

Makes no difference.

There is very little of the C++ syntax and semantics used in Arduino.

In C++ you might write:

serial.write(.....);

In C you could write:

write(serial, ....);

When I have inspected such code in C and C++ I was amazed to find that C++ generated, byte for byte, the same instructions as C.

3 Likes

If you compile Rust to WebAssembly, then it might be possible to compile it to C using wasm2c.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.