Of the following languages, which is easiest for Rust to interoperate with? C#, C++, Java

I currently work on a C++ Thrift Server.. reason it's in C++ is one of the dependencies is an API library provided by the vendor. I was looking at the amount of time I've spent learning C++ and pretty happy but.. wanting to see.. can I switch to writing a Thrift Server in Rust? and if I switch to Rust which library SDK would be easiest to try?

They give C++, Java, and C# ... seems java has the most "hits" on google search "How to call Java from Rust" .. but would prefer to talk with the community before I go dive in and try over a weekend.

Thank you

References :

https://interactivebrokers.github.io/tws-api/introduction.html

I don't think ease of interoperation correlates with the number of questions; if anything, the converse is true.

Of these languages, I think far the easiest would be C++, because C++ shares a considerable subset with C; in particular, using extern "C" functions, you can directly interface with Rust via FFI. (Practically speaking, when doing FFI, basically any language needs to pretend it's C at some level, with the appropriate calling convention.)

2 Likes

Rust works really well with C++ because both languages compile to native code, are directly compatible with the C ABI, and share a lot of concepts (RAII, destructors, raw pointers and references, objects don't need to be heap-allocated, etc.).

It's also easy for Rust to go back and forth with C++ code because neither language have a "runtime" which imposes its will on the code being executed. The cxx crate makes this interop pretty ergonomic.

Calling Java or C# from Rust can be a bit of a pain because you need to embed their runtime in your app, and the process for setting that up and getting the build system working consistently is often non-trivial.

I'd recommend using an existing library like the jni crate if you want your Rust code to call a Java library.

I'm not sure which crate you'd use if your Rust code wants to call a C# dependency because I've only ever had to go in the other direction (a C# app calling a Rust DLL using P/Invoke).

3 Likes