5 ms·
I recently wondered whether it's possible to compile rust into a dll/so or whether there is a way to call rust from other languages (e.g. C, R, or ruby). All I
by stewbrew 13y ago
I recently wondered whether it's possible to compile rust into a dll/so or whether there is a way to call rust from other languages (e.g. C, R, or ruby). All I found is that this isn't (easily?) possible because of rust's runtime. Is this true? If so, will it be possible to call rust from, e.g., C code? I'd like to have an alternative to c/c++ for writing native extensions for interpreted languages.
- Dewie 13y agoAlternatively you could write runtime-less Rust. I don't know if the offerings for this at this point is at the prototype stage or better, but it seems like something that they want to keep.
- rcxdude 13y agoIt's on the roadmap: there's plans to split the standard library into parts which require the runtime and parts which don't (as well as into parts which don't require malloc). As it stands you can write them, but you need to avoid parts of the standard library.
- steveklabnik 13y agoThe third production deployment of Rust is a Ruby extension written in Rust. https://air.mozilla.org/sprocketnes-practical-systems-programming-in-rust/ https://air.mozilla.org/sprocketnes-practical-systems-progra... (You'll have to scroll forward) And technically, it's a Ruby extension written in C. But the C is very small, and is mostly written in Rust. This is because Ruby's cexts use so many C macros, it was easier to make a shim then try to port them all to Rust.
- masklinn 13y ago> If so, will it be possible to call rust from, e.g., C code? I'd like to have an alternative to c/c++ for writing native extensions for interpreted languages. Yes. Technically it's already possible[0][1] but AFAIK it requires throwing out all (?) of the stdlib. rust-core[2] is an exploration of a runtime-less subset of the stdlib, which would ultimately be usable under a no_rt flag or something like that (at this point you have to disable the stdlib entirely and use rust-core instead) [0] https://github.com/mozilla/rust/issues/3608 https://github.com/mozilla/rust/issues/3608 [1] https://github.com/charliesome/rustboot https://github.com/charliesome/rustboot [2] https://github.com/thestinger/rust-core https://github.com/thestinger/rust-core
- dbaupp 13y agoYes, it is possible to do it by just avoiding the runtime, e.g. the following works fine: #![crate_id="example_c"] #![crate_type="dylib"] #[no_mangle] pub extern fn my_rust_function(x: i32, y: i32) -> i32 { x + y } Then compiling that gives a libexample_c....so file, which can be linked against the following C: #include<stdio.h> extern int my_rust_function(int, int); int main() { printf("%d", my_rust_function(1, 2)); return 0; } printing 3. (There's no interaction with a runtime here at all.) In fact, it's even possible to "manually" start a runtime[1] inside another process (and if it's started on its own thread then I think it will work flawlessly, if not, then it might break in some corner cases). [1]: http://static.rust-lang.org/doc/master/guide-runtime.html http://static.rust-lang.org/doc/master/guide-runtime.html