Last active
March 12, 2018 12:52
-
-
Save crmaxx/89080996aa542b93085b74ff3f4d9f39 to your computer and use it in GitHub Desktop.
example for trait from Telegram
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment