November 2017
Intermediate to advanced
264 pages
5h 45m
English
Suppose we want to calculate the tangents of a complex number. The num crate offers this functionality; however, for the purposes of this exercise, we will call the ctanf function from the C library libm instead. This is a collection of mathematical functions implemented in C. The following code defines a complex number as a simple struct.
// code from Chapter 10/code/calling_clibrary.rs:
#[repr(C)]
#[derive(Copy, Clone)]
#[derive(Debug)]
struct Complex {
re: f32,
im: f32,
}
#[link(name = "m")]extern { fn ctanf(z: Complex) -> Complex;}
fn tan(z: Complex) -> Complex {
unsafe { ctanf(z) } } fn main() { let z = Complex { re: -1., im: 1. }; // z is -1 + i let ...Read now
Unlock full access