Trait UnescapeExt

Source
pub trait UnescapeExt: Sealed {
    // Required methods
    fn to_unescaped(&self) -> Result<Cow<'_, str>, InvalidEscape>;
    fn to_unescaped_with(
        &self,
        callback: impl EscapeHandler,
    ) -> Result<Cow<'_, str>, InvalidEscape>;
}
Expand description

An extension trait for &str to allow parsing escape sequences in strings, only copying when needed.

Required Methods§

Source

fn to_unescaped(&self) -> Result<Cow<'_, str>, InvalidEscape>

Unescapes a string, returning an alloc::borrow::Cow. Will only allocate if the string has any escape sequences.

Uses crate::DefaultHandler.

§Errors

Errors if there’s an invalid escape sequence in the string. Passes back the byte index of the invalid character.

§Examples
§Parsing an escaped string
let escaped = "Hello,\\nworld!".to_unescaped();
assert_eq!(
    escaped.unwrap(),
    Cow::Owned::<'_, str>("Hello,\nworld!".to_string())
);
§Not allocating for a string without escapes
let no_escapes = "No escapes here!".to_unescaped();
assert_eq!(
    no_escapes.unwrap(),
    Cow::Borrowed("No escapes here!")
);
§Erroring for invalid escapes
//                            v  invalid at index 7
let invalid_escape = r"Uh oh! \xJJ".to_unescaped();
assert_eq!(
    invalid_escape.unwrap_err().index,
    7
);
Source

fn to_unescaped_with( &self, callback: impl EscapeHandler, ) -> Result<Cow<'_, str>, InvalidEscape>

Unescapes a string using a custom escape handler. See the documentation of crate::EscapeHandler for more details.

§Errors

Errors if there’s an invalid escape sequence in the string. Passes back the byte index of the invalid character.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl UnescapeExt for str

Implementors§