/// Take a character-based slice of a string (as opposed to the default byte-slice).
/// Allows negative indices to slice from the end (but start must be before end).
/// This may not be very fast.
Take a character-based slice of a string (as opposed to the default byte-slice).
Allows negative indices to slice from the end (but start must be before end).
This may not be very fast.
Take a character - based slice of a string ( as opposed to the default byte - slice ) . Allows negative indices to slice from the end ( but start must be before end ) . This may not be very fast .
pub fn charslice<S: Into<String>>(text: S, start: isize, end: isize) -> String {
    let stext = text.into();
    let from: usize;
    let length: usize;
    let charcount = stext.chars().count();
    if start < 0 {
        // LATER: may remove this check and just default to 0 in the future.
        assert!(
            -start as usize <= charcount,
            "charslice: if 'start' is negative, the magnitude may not exceed the length"
        );
        from = (charcount as isize + start) as usize;
    } else {
        from = start as usize;
    }
    if end < 0 {
        // LATER: may remove this check and just default to 0 in the future.
        assert!(
            -end as usize <= charcount,
            "charslice: if 'end' is negative, the magnitude may not exceed the length"
        );
        let new_end = (charcount as isize + end) as usize;
        assert!(new_end >= from, "charslice: 'start' may not be before 'end' (end was negative)");
        length = new_end - from;
    } else {
        assert!(
            end >= from as isize,
            "charslice: 'start' may not be before 'end' (end was positive)"
        );
        length = end as usize - from;
    }
    stext.chars().skip(from).take(length).collect()
}
pub fn charslice < S : Into < String > > ( text : S , start : isize , end : isize ) -> String { let stext = text . into ( ) ; let from : usize ; let length : usize ; let charcount = stext . chars ( ) . count ( ) ; if start < 0 { // LATER: may remove this check and just default to 0 in the future. assert ! ( start as usize charcount " " ) ; from = ( charcount as isize + start ) as usize ; } else { from = start as usize ; } if end < 0 { // LATER: may remove this check and just default to 0 in the future. assert ! ( end as usize charcount " " ) ; let new_end = ( charcount as isize + end ) as usize ; assert ! ( new_end from " " ) ; length = new_end - from ; } else { assert ! ( end from as isize " " ) ; length = end as usize - from ; } stext . chars ( ) . skip ( from ) . take ( length ) . collect ( ) }
Take a character-based slice of a string (as opposed to the default byte-slice).
Take a character - based slice of a string ( as opposed to the default byte - slice ) .
text
S
start
isize
end
isize