在Rust中使用C语言的库功能

一曲冷凌霜 提交于 2020-01-25 09:16:05

主要是了解unsafe{}语法块的作用。

 

#[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 z_tan = tan(z);
  println!("the tangens of {:?} is {:?}", z, z_tan);
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!