Skip to content

Commit d32a262

Browse files
authored
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.
2 parents c25975d + d9ec5fa commit d32a262

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/liballoc/string.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1402,16 +1402,21 @@ impl String {
14021402
&mut self.vec
14031403
}
14041404

1405-
/// Returns the length of this `String`, in bytes.
1405+
/// Returns the length of this `String`, in bytes, not [`char`]s or
1406+
/// graphemes. In other words, it may not be what a human considers the
1407+
/// length of the string.
14061408
///
14071409
/// # Examples
14081410
///
14091411
/// Basic usage:
14101412
///
14111413
/// ```
14121414
/// let a = String::from("foo");
1413-
///
14141415
/// assert_eq!(a.len(), 3);
1416+
///
1417+
/// let fancy_f = String::from("ƒoo");
1418+
/// assert_eq!(fancy_f.len(), 4);
1419+
/// assert_eq!(fancy_f.chars().count(), 3);
14151420
/// ```
14161421
#[inline]
14171422
#[stable(feature = "rust1", since = "1.0.0")]

src/libcore/str/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2085,8 +2085,8 @@ impl str {
20852085
/// let len = "foo".len();
20862086
/// assert_eq!(3, len);
20872087
///
2088-
/// let len = "ƒoo".len(); // fancy f!
2089-
/// assert_eq!(4, len);
2088+
/// assert_eq!("ƒoo".len(), 4); // fancy f!
2089+
/// assert_eq!("ƒoo".chars().count(), 3);
20902090
/// ```
20912091
#[stable(feature = "rust1", since = "1.0.0")]
20922092
#[inline]

0 commit comments

Comments
 (0)