rust - Implement slice_shift_char using the std library -


i'd use &str method slice_shift_char, marked unstable in documentation:

unstable: awaiting conventions shifting , slices , may not warranted existence of chars and/or char_indices iterators

what way implement method, rust's current std library? far have:

fn slice_shift_char(s: &str) -> option<(char, &str)> {     let mut ixs = s.char_indices();     let next = ixs.next();     match next {         some((next_pos, ch)) => {             let rest = unsafe {                 s.slice_unchecked(next_pos, s.len())             };             some((ch, rest))         },         none => none     } } 

i'd avoid call slice_unchecked. i'm using rust 1.1.

well, can @ source code, , you'll https://github.com/rust-lang/rust/blob/master/src/libcollections/str.rs#l776-l778 , https://github.com/rust-lang/rust/blob/master/src/libcore/str/mod.rs#l1531-l1539 . second:

fn slice_shift_char(&self) -> option<(char, &str)> {     if self.is_empty() {         none     } else {         let ch = self.char_at(0);         let next_s = unsafe { self.slice_unchecked(ch.len_utf8(), self.len()) };         some((ch, next_s))     } } 

if don't want unsafe, can use normal slice:

fn slice_shift_char(&self) -> option<(char, &str)> {     if self.is_empty() {         none     } else {         let ch = self.char_at(0);         let len = self.len();         let next_s = &self[ch.len_utf8().. len];         some((ch, next_s))     } } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -