That format is UTF-8, and for better or for worse, the null char '\0'
is a valid Unicode char
, which is {en,de}coded as a null byte b'\0'
.
So, what you are suggesting is not that different from saying that a str
ing created from a UTF-8 sequence encoding "Display
-invisible" char
s at the end ('\0'
being only one of them) should be auto-trimmed, and this gets to logic that should not, imho, be bundled within a "cast" / copyless operation.
Granted, that last point invoked a subjective view, same as you have your legitimate opposing view.
But here comes what is an actual objective rationale:
-
String::from_utf8
and str::from_utf8
ought to have the same beavior (owned / reborrowed variants of the same logic);
-
If str::from_utf8
"dropped" the terminating null, then it would be impossible to go back to a C-string view, unless a copy were to take place. This is unacceptable for a low-level primitive within a systems programming language.
Hence str::from_utf8
is correct in not dropping the terminating nul, and thus String::from_utf8
must also do the same, for the sake of API consistency.
Regarding your situation: you are mainly surprised that a C "string"
is sugar language for Rust's b"string\0"
, and that if you write test / logic between the two languages, then that trailing null is something to be aware of. Interop is rarely 100% smooth, we could say 
The way to go from a C string to Rust structs is through CStr
(CStr::from_ptr
or CStr::from_bytes_with_nul
). Then, you can control whether you want to bundle the terminating null byte with the .to_bytes_with_nul()
/ .to_bytes()
methods, which will yield you the byte slice / view you desire.
-
For your use case, they even bundle a convenience method of .to_str()
which strips the terminating null byte (weirdly enough they don't bundle a .to_str_with_nul()
).
-
Btw, for UTF-8 encoded C strings, you may want to try the char_p::{Ref<'_>, Box}
types, which do feature .to_str{,with_null}()
APIs to give you the choice (in the borrowed case; in the owned case the "choice API" is missing)