相关疑难解决方法(0)

如何将C字符串转换为Rust字符串并通过FFI返回?

我正在尝试获取C库返回的C字符串,并通过FFI将其转换为Rust字符串.

mylib.c

const char* hello(){
    return "Hello World!";
}
Run Code Online (Sandbox Code Playgroud)

main.rs

#![feature(link_args)]

extern crate libc;
use libc::c_char;

#[link_args = "-L . -I . -lmylib"]
extern {
    fn hello() -> *c_char;
}

fn main() {
    //how do I get a str representation of hello() here?
}
Run Code Online (Sandbox Code Playgroud)

c ffi rust

47
推荐指数
2
解决办法
2万
查看次数

标签 统计

c ×1

ffi ×1

rust ×1