Calling rust to c or c to rust


#1

Does it affect on performance of software if i use multiple c codes or library’s in rust or vice varsa. which is the best ? if i linked all languages with rust then is it bad or good. sorry i just thought what happen if i do like this in a program. please explain


#2

There should not be any performance difference. You can link Rust libraries into “C binaries” or the other way around, it doesn’t really matter. To the system, they are basically the same, both native libraries.
You could compare this to linking C++ libraries. Both Rust and C++ have an optional minimal runtime.

@huon for example, says that on his machine it takes ~2ns to call from C into C, or from C into Rust.


#3

our how could you explain about java. java uses J virtual machine ???


#4

It’s harder to communicate with a language that uses a virtual machine, there are also issues with differences in how the memory is handled, for example, in Java usually the memory is leaked and then collected with the help of a garbage collector. How would it interpret memory coming from a native library? - care has to be taken here.

But it is doable, most of the programming languages expose a mechanism to communicate with C, through that mechanism you can talk to those programming languages. Here is the list of a few programming languages(some using a virtual machine, some are native but have name mangling):

Note that calling into a language that uses a virtual machine involves a significant call overhead.


#5

Thank you So much.


#6

There may be a small overhead of calling rust from C; you need to decide if you must catch panics on that boundary, or if no panics are possible.


#7

Same goes for C++. That “minimal runtime” contains exceptions for example.
C doesn’t know about exceptions while C++ has them on by default.
While calling the function doesn’t involve overhead, you do have to take exceptions into considerations when C talks to C++ code. But there is something to take care of when talking to almost any other languages from C, because C doesn’t know of the runtime those languages carry with them.
In general, if you plan to cross the boundaries of different languages/runtimes you need to know first the aspects of those environments(like exceptions, garabage collection…).