[][src]Function snailquote::escape

pub fn escape(s: &str) -> Cow<str>

Escape the provided string with shell-like quoting and escapes. Strings which do not need to be escaped will be returned unchanged.

Details

Escape will prefer to avoid quoting when possible. When quotes are required, it will prefer single quotes (which have simpler semantics, namely no escaping). In all other cases it will use double quotes and escape whatever characters it needs to.

For the full list of escapes which will be used, see the table in unescape.

Examples

use snailquote::escape;
println!("{}", escape("foo")); // no escapes needed
// foo
println!("{}", escape("String with spaces")); // single-quoteable
// 'String with spaces'
println!("{}", escape("東方")); // no escapes needed
// 東方
println!("{}", escape("\"new\nline\"")); // escape needed
// "\"new\nline\""