No, the standard library does not have such a function, but it does not need it.
As pointed out in the comments, raw bytes can be converted to &str through:
None of them perform additional distribution. The first ensures that the bytes are valid UTF-8, the second does not. Everyone should use a validated form until profiling proves that this is a bottleneck, and then use an unverified form once it is safe to do so.
If bytes deeper in the data must be analyzed, a fragment of the raw bytes can be obtained before conversion:
use std::str; fn main() { let raw_data = b"123132"; let the_bytes = &raw_data[1..4]; let the_string = str::from_utf8(the_bytes).expect("not UTF-8"); let the_number: u64 = the_string.parse().expect("not a number"); assert_eq!(the_number, 231); }
As in other code, these lines can be extracted into a function or attribute to allow reuse. However, as soon as this path is followed, it would be nice to study one of the many great boxes designed for parsing . This is especially true if, in addition to text data, binary data needs to be analyzed.
Shepmaster
source share