Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::ffi::CString;
- trait ReadByteString {
- fn read_string_zero(&self) -> Option<CString> ;
- }
- impl ReadByteString for [u8] {
- fn read_string_zero(&self) -> Option<CString> {
- let pos = self.iter().position(|&x| x == 0 )?;
- CString::new(&self[..pos]).ok()
- }
- }
- fn main() {
- let res = b"hello\0hehe"[..].read_string_zero();
- println!("{:?}", res);
- }
Add Comment
Please, Sign In to add comment