pub trait StrExt {
// Required methods
fn quoted(&self) -> QuotedStr<'_>;
fn escaped(&self) -> EscapedStr<'_>;
}
Expand description
Extension methods for str
.
Required Methods§
sourcefn quoted(&self) -> QuotedStr<'_>
fn quoted(&self) -> QuotedStr<'_>
Wraps the string slice in a type whose display implementation renders the string surrounded by double quotes with any inner double quote characters escaped.
§Examples
In the standard case, when the wrapped string does not contain any double quote characters:
use mz_ore::str::StrExt;
let name = "bob";
let message = format!("unknown user {}", name.quoted());
assert_eq!(message, r#"unknown user "bob""#);
In a pathological case:
use mz_ore::str::StrExt;
let name = r#"b@d"inp!t""#;
let message = format!("unknown user {}", name.quoted());
assert_eq!(message, r#"unknown user "b@d\"inp!t\"""#);
sourcefn escaped(&self) -> EscapedStr<'_>
fn escaped(&self) -> EscapedStr<'_>
Same as StrExt::quoted
, but also escapes new lines and tabs.
§Examples
use mz_ore::str::StrExt;
let name = "b@d\"\tinp!t\"\r\n";
let message = format!("unknown user {}", name.escaped());
assert_eq!(message, r#"unknown user "b@d\"\tinp!t\"\r\n""#);