1
Fork 0

Rollup merge of #66038 - jdxcode:char-len, r=alexcrichton

doc(str): show example of chars().count() under len()

the docs are great at explaining that .len() isn't like in other
languages but stops short of explaining how to get the character length.
This commit is contained in:
Pietro Albini 2019-11-05 09:49:56 +01:00 committed by GitHub
commit d32a262ab8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 4 deletions

View file

@ -1402,7 +1402,9 @@ impl String {
&mut self.vec
}
/// Returns the length of this `String`, in bytes.
/// Returns the length of this `String`, in bytes, not [`char`]s or
/// graphemes. In other words, it may not be what a human considers the
/// length of the string.
///
/// # Examples
///
@ -1410,8 +1412,11 @@ impl String {
///
/// ```
/// let a = String::from("foo");
///
/// assert_eq!(a.len(), 3);
///
/// let fancy_f = String::from("ƒoo");
/// assert_eq!(fancy_f.len(), 4);
/// assert_eq!(fancy_f.chars().count(), 3);
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]

View file

@ -2085,8 +2085,8 @@ impl str {
/// let len = "foo".len();
/// assert_eq!(3, len);
///
/// let len = "ƒoo".len(); // fancy f!
/// assert_eq!(4, len);
/// assert_eq!("ƒoo".len(), 4); // fancy f!
/// assert_eq!("ƒoo".chars().count(), 3);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]