小编Moc*_*ida的帖子

为什么 Rust 中的 C strlen() 也会计算 print 中的字符串切片!`s` 变量之后的宏?

所以我只是在 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() …

c libc strlen rust

1
推荐指数
1
解决办法
490
查看次数

标签 统计

c ×1

libc ×1

rust ×1

strlen ×1