News for hackers. Stuff that matters.
Re:Rust for C++ programmers – part 4: unique pointers by stewbrew on Tuesday April 29, 2014 @06:14AM (#7665992)
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.
Re:Rust for C++ programmers – part 4: unique pointers by dbaupp on Tuesday April 29, 2014 @08:09AM (#7666364)
Yes, 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

Re:Rust for C++ programmers – part 4: unique pointers by masklinn on Tuesday April 29, 2014 @08:03AM (#7666344)
> 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

[1] https://github.com/charliesome/rustboot

[2] https://github.com/thestinger/rust-core

Re:Rust for C++ programmers – part 4: unique pointers by rcxdude on Tuesday April 29, 2014 @07:13AM (#7666174)
It'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.
Re:Rust for C++ programmers – part 4: unique pointers by steveklabnik on Tuesday April 29, 2014 @07:48AM (#7666298)
The third production deployment of Rust is a Ruby extension written 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.

Re:Rust for C++ programmers – part 4: unique pointers by Dewie on Tuesday April 29, 2014 @06:20AM (#7666013)
Alternatively 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.