0

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);
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 1
    Once you have a pointer, you can cast it as you like. You can take your `*mut i8` and make it into a `*const u8`, for example. [Applied to your example](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=32e9ce436e92a6ff443b7393ac21e766) – Shepmaster Nov 24 '21 at 19:55
  • Thank you so much! it worked – Ivan Raul Sanchez Diaz Nov 24 '21 at 19:56

0 Answers0