fn replace_escapes(text: &str) -> Result<String, &'static str> {
    let mut result = String::new();
    let mut iter = text.chars();
    while let Some(ch) = iter.next() {
        if ch == '\\' {
            if let Some(ch) = iter.next() {
                match ch {
                    'a' => result.push('\x07'),
                    'b' => result.push('\x08'),
                    't' => result.push('\t'),
                    'n' => result.push('\n'),
                    'r' => result.push('\r'),
                    '\\' => result.push('\\'),
                    'x' => {
                        let mut hex = String::new();
                        loop {
                            if let Some(ch) = iter.next() {
                                if ch == ';' {
                                    break;
                                }
                                hex.push(ch);
                            } else {
                                return Err("missing ; after \\x escape sequence");
                            }
                        }
                        // verify this is a valid inline hex escape value
                        match u32::from_str_radix(&hex[..], 16) {
                            Ok(code) => match char::from_u32(code) {
                                Some(x) => result.push(x),
                                None => {
                                    return Err("invalid UTF code point");
                                }
                            },
                            Err(_) => {
                                return Err("invalid hexadecimal escape code");
                            }
                        }
                    }
                    _ => {
                        return Err("expected x|a|b|t|n|r after \\ in escape sequence");
                    }
                }
            } else {
                return Err("reached EOF after \\ escape");
            }
        } else {
            result.push(ch);
        }
    }
    Ok(result)
}
/// `replace_escapes` replaces any \xNNNN; escape sequences with the Unicode
/// code point identified by the NNNN hexadecimal value, where NNNN can be
/// two, three, or four hexadecimal digits. The code point must be valid.
/// Also handles the \a, \b, \t, \n, and \r escapes.
`replace_escapes` replaces any \xNNNN; escape sequences with the Unicode
code point identified by the NNNN hexadecimal value, where NNNN can be
two, three, or four hexadecimal digits. The code point must be valid.
` replace_escapes ` replaces any \ xNNNN ; escape sequences with the Unicode code point identified by the NNNN hexadecimal value where NNNN can be two three or four hexadecimal digits . The code point must be valid .
fn replace_escapes(text: &str) -> Result<String, &'static str> {
    let mut result = String::new();
    let mut iter = text.chars();
    while let Some(ch) = iter.next() {
        if ch == '\\' {
            if let Some(ch) = iter.next() {
                match ch {
                    'a' => result.push('\x07'),
                    'b' => result.push('\x08'),
                    't' => result.push('\t'),
                    'n' => result.push('\n'),
                    'r' => result.push('\r'),
                    '\\' => result.push('\\'),
                    'x' => {
                        let mut hex = String::new();
                        loop {
                            if let Some(ch) = iter.next() {
                                if ch == ';' {
                                    break;
                                }
                                hex.push(ch);
                            } else {
                                return Err("missing ; after \\x escape sequence");
                            }
                        }
                        // verify this is a valid inline hex escape value
                        match u32::from_str_radix(&hex[..], 16) {
                            Ok(code) => match char::from_u32(code) {
                                Some(x) => result.push(x),
                                None => {
                                    return Err("invalid UTF code point");
                                }
                            },
                            Err(_) => {
                                return Err("invalid hexadecimal escape code");
                            }
                        }
                    }
                    _ => {
                        return Err("expected x|a|b|t|n|r after \\ in escape sequence");
                    }
                }
            } else {
                return Err("reached EOF after \\ escape");
            }
        } else {
            result.push(ch);
        }
    }
    Ok(result)
}
fn replace_escapes ( text : & str ) -> Result < String , & ' static str > { let mut result = String :: new ( ) ; let mut iter = text . chars ( ) ; while let Some ( ch ) = iter . next ( ) { if ch == '\\' { if let Some ( ch ) = iter . next ( ) { match ch { 'a' => result . push ( '\x07' ) , 'b' => result . push ( '\x08' ) , 't' => result . push ( '\t' ) , 'n' => result . push ( '\n' ) , 'r' => result . push ( '\r' ) , '\\' => result . push ( '\\' ) , 'x' => { let mut hex = String :: new ( ) ; loop { if let Some ( ch ) = iter . next ( ) { if ch == ';' { break ; } hex . push ( ch ) ; } else { return Err ( " \\ " ) ; } } // verify this is a valid inline hex escape value match u32 :: from_str_radix ( & hex [ .. ] , 16 ) { Ok ( code ) => match char :: from_u32 ( code ) { Some ( x ) => result . push ( x ) , None => { return Err ( " " ) ; } } , Err ( _ ) => { return Err ( " " ) ; } } } _ => { return Err ( " \\ " ) ; } } } else { return Err ( " \\ " ) ; } } else { result . push ( ch ) ; } } Ok ( result ) }
`replace_escapes` replaces any \xNNNN; escape sequences with the Unicode
code point identified by the NNNN hexadecimal value, where NNNN can be
two, three, or four hexadecimal digits.
` replace_escapes ` replaces any \ xNNNN ; escape sequences with the Unicode code point identified by the NNNN hexadecimal value where NNNN can be two three or four hexadecimal digits .
text
&str