#![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
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
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.