If you're talking about doing it at the beginning of the program, it's probably possible, but hard. According to Linkage - The Rust Reference , you can compile with --crate-type=dylib. Unfortunately, Rust's ABI isn't stable so you can't load code between versions. I found this Reddit thread about it where people say it's possible but don't really explain how to do it.
If you want to load code in the middle of the program, that's tricky too. I don't think anyone's made a dlopen/ClassLoader sort of thing for loading rust code.
But it is possible to use --crate-type=cdylib, and then mark some functions as
#[no_mangle]
pub extern "C" fn my_fn() {
}
this causes Rust to build a C-style dynamic library. And extern "C" functions with #[no_mangle] can be loaded from the library. A crate like dlopen or libloading can dynamically load the cdylib.
I made a proof-of-concept for an internet simulator that uses this. It uses the dlopen crate. My idea was that users could create node plugins that the main code could use. It worked as intended! But there might have been some safety issue I missed. Loading code Plugin code
Yeah the safe issue is a bit new to me in this respect.
I mean could you write the bulk of the code to be safe with the exception of the dynamic linking part?
The idea would be to have some sort of stub that does a bunch of common code but then depending on user input of in response to an event, dynamic loading of code occurs where your loading the code based on some sort of URL
Its been over a decade since I last looked at this.
Just trying to see what could be done…