Rust provides a Foreign Function Interface (FFI) to C libraries. Foreign
functions must be declared inside an extern
block annotated with a #[link]
attribute containing the name of the foreign library.
// ffi.rs
use std::fmt;
// this extern block links to the libm library
#[link(name = "m")]
extern {
// this is a foreign function
// that computes the square root of a single precision complex number
fn csqrtf(z: Complex) -> Complex;
}
fn main() {
// z = -1 + 0i
let z = Complex { re: -1., im: 0. };
// calling a foreign function is an unsafe operation
let z_sqrt = unsafe {
csqrtf(z)
};
println!("the square root of {} is {}", z, z_sqrt);
}
// Minimal implementation of single precision complex numbers
#[repr(C)]
#[deriving(Copy)]
struct Complex {
re: f32,
im: f32,
}
impl fmt::Show for Complex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.im < 0. {
write!(f, "{}-{}i", self.re, -self.im)
} else {
write!(f, "{}+{}i", self.re, self.im)
}
}
}
{ffi.out}
Since calling foreign functions is considered unsafe, it's common to write safe wrappers around them.
// safe.rs
use std::fmt;
#[link(name = "m")]
extern {
fn ccosf(z: Complex) -> Complex;
}
// safe wrapper
fn cos(z: Complex) -> Complex {
unsafe { ccosf(z) }
}
fn main() {
// z = 0 + 1i
let z = Complex { re: 0., im: 1. };
println!("cos({}) = {}", z, cos(z));
}
// Minimal implementation of single precision complex numbers
#[repr(C)]
#[deriving(Copy)]
struct Complex {
re: f32,
im: f32,
}
impl fmt::Show for Complex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.im < 0. {
write!(f, "{}-{}i", self.re, -self.im)
} else {
write!(f, "{}+{}i", self.re, self.im)
}
}
}
{safe.out}