Can Rust compile to C and C++

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.