From d9ec5fa88ca0c082aa2089b372e7981f9d514e82 Mon Sep 17 00:00:00 2001 From: Jeff Dickey <216188+jdxcode@users.noreply.github.com> Date: Fri, 1 Nov 2019 19:42:33 -0700 Subject: [PATCH] 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. r? @steveklabnik --- src/liballoc/string.rs | 9 +++++++-- src/libcore/str/mod.rs | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/liballoc/string.rs b/src/liballoc/string.rs index d9927c642b2d8..0e5746d0d9df0 100644 --- a/src/liballoc/string.rs +++ b/src/liballoc/string.rs @@ -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")] diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index f67012d8f2fce..1968919f5541c 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -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]