5 ms·
What's the point of using C++ in Rust when you could use Rust instead and not have to add a cargo package?
by netr0ute 5y ago
What's the point of using C++ in Rust when you could use Rust instead and not have to add a cargo package?
- sgeisenh 5y agoThere is a lot of code written in C++. For a lot of large companies, one of the impediments to migrating from C++ to Rust is the investment required to expose the existing C++ interfaces in Rust. There are also many open source libraries written in C++ that you can more easily call from Rust using this bridge library.
- tux3 5y agoIt's not just about using C++ libraries from Rust, but also the other way around. You have 15 million lines of C++ code, and you want to start writing some components in Rust. At the interface, you need a bridge.
- deleted 5y ago[deleted]
- deleted 5y ago[deleted]
- deadcore 5y agoIf you have existing resources written in C++, external libs, maybe you've created a 'plugin' which is loaded by an application but the plugin is written in Rust and the app is written in C++ and it needs to invoke back into the main applications code. Just examples of stuff where I've had to interop Rust with other things outside of my control :)
- ben-schaaf 5y agoThere's lots of useful C++ libraries that don't have (good) rust bindings.
- onceiwasthere 5y agoI guess if you already have C++ that you'd prefer not to re-write. Also things like graphics programming, while making great strides in rust, are definitely still superior in C++ in certain aspects so you might want to write, say, an OpenGL interface in C++.
- pjmlp 5y agoPlenty of ecosystems where C++ rules and no one is going to rewrite any of those libraries.
- mcronce 5y agoIt takes a lot less time than rewriting [component] in Rust
- detaro 5y agoRewriting a few million lines of existing C++ is not cheap nor quickly done.
- codetrotter 5y agoI have a pretty cool use case where I successfully interfaced between C++ and Rust code. This was for a project consisting of a sculpture with a bunch of LED strips on it, and my part was to do the programming, electronics and wiring for it. To control the LED strips I use Teensy 3.2 microcontroller boards, with the OctoWS2811 adaptor board [0], along with an industrial singleboard computer. The Teensy 3.2 boards I programmed using the Arduino libraries and the OctoWS2811 library [1]. The code I wrote for this part was C++. (But I don’t use the Arduino IDE, I use JetBrains CLion.) Compiling and flashing the firmware for the Teensy, and disconnecting and reconnecting cables etc was making the development a bit annoying. So in Rust I wrote a LED simulator program that renders the “pixels” of the LED strips on my screen, and I compiled my C++ code that I’ve written for the microcontrollers on the host, with some shims that I wrote to provide some functions that my firmware uses, and I link this with my Rust program. The end result is that I have a fully functioning LED simulator that I use while developing, that runs on my computer. It runs on both macOS and Linux. And then the same C++ code of mine gets included and built without modification when I build my firmware for the microcontrollers. It is work that I am very satisfied about :) Should also note that I use the bindgen crate, not cxx. https://crates.io/crates/bindgen https://crates.io/crates/bindgen [0]: https://www.pjrc.com/store/octo28_adaptor.html https://www.pjrc.com/store/octo28_adaptor.html [1]: https://www.pjrc.com/teensy/td_libs_OctoWS2811.html https://www.pjrc.com/teensy/td_libs_OctoWS2811.html
- chris_overseas 5y agoThis sounds great, I'd been thinking about creating something similar (I also have a project with Teensy 3.2/4.0, OctoWS2811 and CLion). Is there any chance you'd be willing to open source it? In particular I'd be interested in what you did to get the Teensy code compiling and running on the host with the shims you mention.
- girvo 5y agoIf your board supports it, the Zephyr RTOS and build tools support building your firmware for a Linux binary target too :) even has a neat RPC protocol to test the firmware on your computer rather than on the board. Probably too much for a Teensy tho, but it is supported: https://docs.zephyrproject.org/latest/boards/arm/teensy4/doc/index.html https://docs.zephyrproject.org/latest/boards/arm/teensy4/doc...