所以我只是在 Rust 中修改 C 库,我发现以下代码:
extern crate libc;
use libc::{c_char, c_int, size_t};
extern "C" {
fn printf(fmt: *const c_char, ...) -> c_int;
fn strlen(arr: *const c_char) -> size_t;
}
fn main() {
unsafe {
printf("This uses C's standard lib printf".as_ptr() as *const i8);
print!("\n");
let s = "Useless thing again";
print!("Length of {}: ", s);
let x = strlen(s.as_ptr() as *const i8);
print!("{}", &x);
}
}
Run Code Online (Sandbox Code Playgroud)
将产生这个:
This uses C's standard lib printf
Length of Useless thing again: 31
Run Code Online (Sandbox Code Playgroud)
strlen() …