I am trying to use a C library that requires me to convert strings into *mut i8
. Everything is fine, but I want to print the strings with println!
. My string is printed something like Connecting to: 0x0000559c206d99f0
.
This is my code (from https://stackoverflow.com/a/67560868/15725012):
let string: &str = "localhost";
let bytes: Vec<u8> = String::from(string).into_bytes();
let mut c_chars: Vec<i8> = bytes.iter().map(|c| *c as i8).collect::<Vec<i8>>();
c_chars.push(0);
let ip: *mut raw::c_char = c_chars.as_mut_ptr();
println!("Connecting to:{:#?}", ip);