How to Use CString and CStr for FFI String Handling

Use CString::new to convert Rust strings to C-compatible null-terminated strings and CStr::from_ptr to safely read C strings back into Rust.

Use CString::new to convert a Rust string to a C-compatible null-terminated string for FFI, and CStr::from_ptr to safely read a C string back into Rust.

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

// Rust to C: Create a CString
let rust_str = "Hello, C!";
let c_str = CString::new(rust_str).unwrap();
let c_ptr: *const c_char = c_str.as_ptr();

// C to Rust: Read a C string (assuming c_ptr is valid)
let c_str_ref = unsafe { CStr::from_ptr(c_ptr) };
let rust_string = c_str_ref.to_string_lossy().to_string();