主要是了解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);
}
转载于:https://www.cnblogs.com/aguncn/p/11481272.html
相关资源:cpp-rustcxx能够很方便地在Rust中使用C