# Dynamically loading and calling Rust code

**URL:** <https://users.rust-lang.org/t/dynamically-loading-and-calling-rust-code/43230>\
**Category:** help\
**Created:** [May 25, 2020, 9:32pm UTC](https://users.rust-lang.org/t/dynamically-loading-and-calling-rust-code/43230 "2020-05-25T21:32:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mankinskin](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mankinskin/32/46764_2.png) [@mankinskin](https://users.rust-lang.org/u/mankinskin)\
**Post date:** [May 25, 2020, 9:32pm UTC](https://users.rust-lang.org/t/dynamically-loading-and-calling-rust-code/43230/1 "2020-05-25T21:32:26Z")

</div>

Hi, I am looking for some help with understanding how dynamic loading works in Rust.  
What I understand is that I can use the FFI to link extern libraries at runtime. Now my question is how practical this is for my use-case.  
Say I have a client-server architecture, and I want to be able to receive Rust code from the client or server, compile it and link it with the running server or wasm client, then execute some code in it.

Would that be possible with a reasonable amount of overhead? Of course the compiler needs to run sometime, or are there some kind of "rusty" interpreted languages I could run through an interpreter?

Does anyone maybe have some experience with this or could someone explain to me how something like this would work?

Thanks a lot

---

<div class="post-metadata">

**Author:** ![sanxiyn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/sanxiyn/32/24284_2.png) [@sanxiyn](https://users.rust-lang.org/u/sanxiyn)\
**Post date:** [May 26, 2020, 12:58am UTC](https://users.rust-lang.org/t/dynamically-loading-and-calling-rust-code/43230/2 "2020-05-26T00:58:28Z")

</div>

Currently Rust is pretty unsuitable for this use case. How about using JavaScript? I guess it is not "rusty", but then you didn't define what that is, so I am at loss.

---

<div class="post-metadata">

**Author:** ![2ndTaleStudio](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/2ndtalestudio/32/12889_2.png) [@2ndTaleStudio](https://users.rust-lang.org/u/2ndTaleStudio)\
**Post date:** [May 26, 2020, 7:44am UTC](https://users.rust-lang.org/t/dynamically-loading-and-calling-rust-code/43230/3 "2020-05-26T07:44:35Z")

</div>

Hi there,  
are you looking for something like a "Plugin" system ?  
On another note this [thread](https://users.rust-lang.org/t/how-to-load-switch-libraries-at-runtime/41106) or this [blog](http://adventures.michaelfbryan.com/posts/plugins-in-rust/)  
might also give some answers for you - however, most of those approaches does not involve JIT compilation but assume the dynamic library is already build.

---

<div class="post-metadata">

**Author:** ![daboross](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/daboross/32/43777_2.png) [@daboross](https://users.rust-lang.org/u/daboross)\
**Post date:** [May 26, 2020, 9:12am UTC](https://users.rust-lang.org/t/dynamically-loading-and-calling-rust-code/43230/4 "2020-05-26T09:12:29Z")

</div>

As others have mentioned, this isn't a use case well supported by Rust. It's _possible_, but I don't think any good interfaces exist.

There are two hard parts here: first, compiling it, and second, linking it with the existing server.

For the first part, I think your best bets are either to just run `rustc` manually, using the CLI interface, or to have your clients compile the rust code before sending it to the server. Depending on what your clients are, one of these could be easier than the other.

For the second part, the most direct choice would be to compile to a dynamic library, and use [libloading](https://github.com/nagisa/rust_libloading). You get direct calls to dynamically loaded dylibs, but the downside is that _all_ of those calls need to be through a C interface, and you have the other constraints of FFI like needing to keep track of which side allocated things, and only free them on that side. [abi\_stable](https://docs.rs/abi_stable/) is a great rust crate for this, but even with that, it can still be clunky.

Another alternative would be to run your compiled rust code as a subprocess, and use an RPC interface to it. Kind of like how text editors use an RPC to talk to the language server - the language server runs as an independent binary, and the text editor uses it as a plugin by running that binary and talking to it via stdin/stdout. This does induce overhead. However, if there's any chance your code will crash, then this is definitely safer - crashing compiled code won't crash your whole service.

From your description, it seems like there might be more concerns, though. Do you trust your clients - in other words, are you willing to give them full user privileges on your machine? If so, then it's all good. If not, you probably want to look into sandboxing both the rustc execution, and the process you run the code in.

---

<div class="post-metadata">

**Author:** ![parasyte](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/parasyte/32/24763_2.png) [@parasyte](https://users.rust-lang.org/u/parasyte)\
**Post date:** [May 26, 2020, 9:30am UTC](https://users.rust-lang.org/t/dynamically-loading-and-calling-rust-code/43230/5 "2020-05-26T09:30:52Z")

</div>

It might be worth trying to use WASM for this kind of use case. [Michael-F-Bryan](http://adventures.michaelfbryan.com/posts/wasm-as-a-platform-for-abstraction/)

---

<div class="post-metadata">

**Author:** ![kornel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kornel/32/4823_2.png) [@kornel](https://users.rust-lang.org/u/kornel)\
**Post date:** [May 26, 2020, 12:57pm UTC](https://users.rust-lang.org/t/dynamically-loading-and-calling-rust-code/43230/6 "2020-05-26T12:57:28Z")

</div>

You can make Rust a dynamic library (`crate-type` of `cdylib`) with a **C API** and use that with [https://lib.rs/dlopen](https://lib.rs/dlopen) or [https://lib.rs/libloading](https://lib.rs/libloading)

---

<div class="post-metadata">

**Author:** ![mankinskin](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mankinskin/32/46764_2.png) [@mankinskin](https://users.rust-lang.org/u/mankinskin)\
**Post date:** [May 26, 2020, 7:00pm UTC](https://users.rust-lang.org/t/dynamically-loading-and-calling-rust-code/43230/7 "2020-05-26T19:00:30Z")

</div>

Thanks to everyone. These solutions are still very much more "static" than interpreting code, so I am not yet sure if this is the right thing for my application.

@sanxiyn With "rusty" interpreted language I mean something that supports the Rust type system, but basically uses a modified reference model which is checked while interpreting. I think this would generally be an extremely valuable extension of Rusts ecosystem.

---

<div class="post-metadata">

**Author:** ![mankinskin](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mankinskin/32/46764_2.png) [@mankinskin](https://users.rust-lang.org/u/mankinskin)\
**Post date:** [May 26, 2020, 7:10pm UTC](https://users.rust-lang.org/t/dynamically-loading-and-calling-rust-code/43230/8 "2020-05-26T19:10:03Z")

</div>

I just found [Scripting languages for Rust](https://github.com/rust-unofficial/awesome-rust#scripting).

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [August 24, 2020, 7:10pm UTC](https://users.rust-lang.org/t/dynamically-loading-and-calling-rust-code/43230/9 "2020-08-24T19:10:06Z")

</div>

Closed on August 24, 2020, 7:10pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
