Guest User

Untitled

a guest
Mar 20th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.37 KB | None | 0 0
  1. use std::ffi::CString;
  2.  
  3. trait ReadByteString {
  4. fn read_string_zero(&self) -> Option<CString> ;
  5. }
  6.  
  7. impl ReadByteString for [u8] {
  8. fn read_string_zero(&self) -> Option<CString> {
  9. let pos = self.iter().position(|&x| x == 0 )?;
  10. CString::new(&self[..pos]).ok()
  11. }
  12. }
  13.  
  14. fn main() {
  15. let res = b"hello\0hehe"[..].read_string_zero();
  16. println!("{:?}", res);
  17. }
Add Comment
Please, Sign In to add comment