Has anyone tried this , does this really work ( playground)

⚓ Rust    📅 2026-02-09    👤 surdeus    👁️ 8      

surdeus

#![crate_type = "cdylib"]

use std::ffi::{CStr, CString};
use std::os::raw::c_char;

/// Example function exported for C
#[no_mangle]
pub extern "C" fn hello_world() -> *mut c_char {
    CString::new("Hello, world!").unwrap().into_raw()
}

/// Free a string allocated by Rust
#[no_mangle]
pub extern "C" fn free_rust_string(s: *mut c_char) {
    if s.is_null() { return; }
    unsafe { CString::from_raw(s); }
}

(Playground)

2 posts - 2 participants

Read full topic

🏷️ Rust_feed