How to convert a non-zero-terminated C string to Rust &str or String


#1

How can I convert a non-zero-terminated C string to Rust &str or String?
I have head: *libc::c_char and size: usize of a non-zero-terminated string.

I think I could malloc a buffer, copy from *libc::c_char to the buffer, then

str::from_utf8(CStr::from_ptr(buf).to_bytes()).unwrap()

Is there a better way?


#2

CStr works with zero-terminated strings, and since you said your string isn’t zero-terminated, that means CStr is the wrong thing to use.
Instead just do:
std::str::from_utf8(std::slice::from_raw_parts(head, size)).unwrap()


#3

(Assuming it’s UTF-8 encoded, of course… :P)


#4

It works great.
Thanks!


#5

Note: this will create an str with an unbound lifetime. Be very careful.


#6

Thanks for the head-up.
After all, I came to use the following code to create a String out of &str.

std::str::from_utf8(std::slice::from_raw_parts(head, size)).unwrap().to_string()

#7

You may want to use .to_owned() method instead of .to_string() to avoid calling heavy weight formatting machinery.


#8

Stupid question: why does to_string call heavy formatting machinery?


#9

Because to_string() is a method of ToString trait, which has impl<T: Display> ToString for T, and as Rust doesn’t support impl specialization now, there’s no way to override this impl for more specific case of &str. And as the only thing the impl<T: Display> ToString for T knows about T is it implements Display, it can use Display::fmt() method only to build string from T, which uses, well, heavy weight formatting machinery.

Maybe in the future Rust, when will get implementations specialization, this impl will be overriden for &str and you won’t have to worry about it any more, but for now there’s str::to_owned() method, created just because of this problem.


#10

We already have an RFC for specialization open


#11

That’s great! It’s not in stable yet, though.


#12

Makes sense, thanks.