extern crate libc; mod go { use libc::c_int; use std::marker::PhantomData; #[link(name = "buildmodes")] extern { fn Multiply(a: c_int, b: c_int) -> c_int; // func Multiply(a, b int) int fn CounterTo(max: c_int) -> *mut (); // func CounterTo(max int) <-chan int fn RecvInt(c: *mut ()) -> c_int; // func RecvInt(c <-chan int) int } pub fn multiply(a: i32, b: i32) -> i32 { unsafe { Multiply(a as c_int, b as c_int) as i32 } } pub trait Recv { type T; fn recv(&self) -> Self::T; } pub struct RecvChan { c: *mut (), phantom: PhantomData } impl RecvChan { pub fn counter_to(max: i32) -> RecvChan { unsafe { RecvChan { c: CounterTo(max), phantom: PhantomData } } } } impl Recv for RecvChan { type T = i32; fn recv(&self) -> i32 { unsafe { RecvInt(self.c) as i32 } } } } fn main() { use go::Recv; println!("Entered Rust main"); // Call a simple function. println!("Calculating 4 * 5 from Go: {}", go::multiply(4, 5)); // Create a channel and receive from it 5 times. let c = go::RecvChan::::counter_to(5); print!("Receiving 5 numbers from a Go channel: "); for _ in 0..5 { print!("{} ", c.recv()); } println!(""); }