@@ -3949,12 +3949,42 @@ impl str {
3949
3949
me. make_ascii_lowercase ( )
3950
3950
}
3951
3951
3952
- /// Escapes each char in `s` with [`char::escape_debug`].
3952
+ /// Return an iterator that escapes each char in `s` with [`char::escape_debug`].
3953
3953
///
3954
3954
/// Note: only extended grapheme codepoints that begin the string will be
3955
3955
/// escaped.
3956
3956
///
3957
3957
/// [`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
+ /// ```
3958
3988
#[ stable( feature = "str_escape" , since = "1.34.0" ) ]
3959
3989
pub fn escape_debug ( & self ) -> EscapeDebug {
3960
3990
let mut chars = self . chars ( ) ;
@@ -3967,17 +3997,77 @@ impl str {
3967
3997
}
3968
3998
}
3969
3999
3970
- /// Escapes each char in `s` with [`char::escape_default`].
4000
+ /// Return an iterator that escapes each char in `s` with [`char::escape_default`].
3971
4001
///
3972
4002
/// [`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
+ /// ```
3973
4033
#[ stable( feature = "str_escape" , since = "1.34.0" ) ]
3974
4034
pub fn escape_default ( & self ) -> EscapeDefault {
3975
4035
EscapeDefault { inner : self . chars ( ) . flat_map ( CharEscapeDefault ) }
3976
4036
}
3977
4037
3978
- /// Escapes each char in `s` with [`char::escape_unicode`].
4038
+ /// Return an iterator that escapes each char in `s` with [`char::escape_unicode`].
3979
4039
///
3980
4040
/// [`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
+ /// ```
3981
4071
#[ stable( feature = "str_escape" , since = "1.34.0" ) ]
3982
4072
pub fn escape_unicode ( & self ) -> EscapeUnicode {
3983
4073
EscapeUnicode { inner : self . chars ( ) . flat_map ( CharEscapeUnicode ) }
0 commit comments