安全模式和不安全模式。
Rust中的不安全操作。
外部函数接口,以及与C语言交互。
使用PyO3与Python交互。
安全与不安全
struct MyType;
unsafe trait UnsafeTrait {
unsafe fn unsafe_func(&self);
fn safe_func(&self) {
println!("Things are fine here!");
}
}
trait SafeTrait {
unsafe fn look_before_you_call(&self);
}
unsafe impl UnsafeTrait for MyType {
unsafe fn unsafe_func(&self) {
println!("Highly unsafe");
}
}
impl SafeTrait for MyType {
unsafe fn look_before_you_call(&self) {
println!("Something unsafe!");
}
}
fn main() {
let my_type = MyType;
my_type.safe_func();
unsafe {
my_type.look_before_you_call();
}
}
在Rust中调用C代码
[build-dependencies]
cc = “1.0”
fn main() {
cc::Build::new().file("mystrle