Skip to content

Commit eb158f9

Browse files
committed
Add doctests for str::escape_*
1 parent 114593d commit eb158f9

File tree

1 file changed

+93
-3
lines changed

1 file changed

+93
-3
lines changed

src/libcore/str/mod.rs

+93-3
Original file line numberDiff line numberDiff line change
@@ -3949,12 +3949,42 @@ impl str {
39493949
me.make_ascii_lowercase()
39503950
}
39513951

3952-
/// Escapes each char in `s` with [`char::escape_debug`].
3952+
/// Return an iterator that escapes each char in `s` with [`char::escape_debug`].
39533953
///
39543954
/// Note: only extended grapheme codepoints that begin the string will be
39553955
/// escaped.
39563956
///
39573957
/// [`char::escape_debug`]: ../std/primitive.char.html#method.escape_debug
3958+
///
3959+
/// # Examples
3960+
///
3961+
/// As an iterator:
3962+
///
3963+
/// ```
3964+
/// for c in "❤\n!".escape_debug() {
3965+
/// print!("{}", c);
3966+
/// }
3967+
/// println!();
3968+
/// ```
3969+
///
3970+
/// Using `println!` directly:
3971+
///
3972+
/// ```
3973+
/// println!("{}", "❤\n!".escape_debug());
3974+
/// ```
3975+
///
3976+
///
3977+
/// Both are equivalent to:
3978+
///
3979+
/// ```
3980+
/// println!("❤\\n!");
3981+
/// ```
3982+
///
3983+
/// Using `to_string`:
3984+
///
3985+
/// ```
3986+
/// assert_eq!("❤\n!".escape_debug().to_string(), "❤\\n!");
3987+
/// ```
39583988
#[stable(feature = "str_escape", since = "1.34.0")]
39593989
pub fn escape_debug(&self) -> EscapeDebug {
39603990
let mut chars = self.chars();
@@ -3967,17 +3997,77 @@ impl str {
39673997
}
39683998
}
39693999

3970-
/// Escapes each char in `s` with [`char::escape_default`].
4000+
/// Return an iterator that escapes each char in `s` with [`char::escape_default`].
39714001
///
39724002
/// [`char::escape_default`]: ../std/primitive.char.html#method.escape_default
4003+
///
4004+
/// # Examples
4005+
///
4006+
/// As an iterator:
4007+
///
4008+
/// ```
4009+
/// for c in "❤\n!".escape_default() {
4010+
/// print!("{}", c);
4011+
/// }
4012+
/// println!();
4013+
/// ```
4014+
///
4015+
/// Using `println!` directly:
4016+
///
4017+
/// ```
4018+
/// println!("{}", "❤\n!".escape_default());
4019+
/// ```
4020+
///
4021+
///
4022+
/// Both are equivalent to:
4023+
///
4024+
/// ```
4025+
/// println!("\\u{{2764}}\n!");
4026+
/// ```
4027+
///
4028+
/// Using `to_string`:
4029+
///
4030+
/// ```
4031+
/// assert_eq!("❤\n!".escape_default().to_string(), "\\u{2764}\\n!");
4032+
/// ```
39734033
#[stable(feature = "str_escape", since = "1.34.0")]
39744034
pub fn escape_default(&self) -> EscapeDefault {
39754035
EscapeDefault { inner: self.chars().flat_map(CharEscapeDefault) }
39764036
}
39774037

3978-
/// Escapes each char in `s` with [`char::escape_unicode`].
4038+
/// Return an iterator that escapes each char in `s` with [`char::escape_unicode`].
39794039
///
39804040
/// [`char::escape_unicode`]: ../std/primitive.char.html#method.escape_unicode
4041+
///
4042+
/// # Examples
4043+
///
4044+
/// As an iterator:
4045+
///
4046+
/// ```
4047+
/// for c in "❤\n!".escape_unicode() {
4048+
/// print!("{}", c);
4049+
/// }
4050+
/// println!();
4051+
/// ```
4052+
///
4053+
/// Using `println!` directly:
4054+
///
4055+
/// ```
4056+
/// println!("{}", "❤\n!".escape_unicode());
4057+
/// ```
4058+
///
4059+
///
4060+
/// Both are equivalent to:
4061+
///
4062+
/// ```
4063+
/// println!("\\u{{2764}}\\u{{a}}\\u{{21}}");
4064+
/// ```
4065+
///
4066+
/// Using `to_string`:
4067+
///
4068+
/// ```
4069+
/// assert_eq!("❤\n!".escape_unicode().to_string(), "\\u{2764}\\u{a}\\u{21}");
4070+
/// ```
39814071
#[stable(feature = "str_escape", since = "1.34.0")]
39824072
pub fn escape_unicode(&self) -> EscapeUnicode {
39834073
EscapeUnicode { inner: self.chars().flat_map(CharEscapeUnicode) }

0 commit comments

Comments
 (0)