From f0c6f5a7feb0a2d14a68133c050ec5d56902f961 Mon Sep 17 00:00:00 2001 From: Venus Xeon-Blonde Date: Wed, 7 Feb 2024 23:29:22 -0500 Subject: [PATCH 1/3] Add documentation on `str::starts_with` Add documentation about a current footgun of `str::starts_with` --- library/core/src/str/mod.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index ecc0613d7b9..b90f34360f6 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -1160,6 +1160,12 @@ pub fn contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool { /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a /// function or closure that determines if a character matches. /// + /// Note that there is a footgun to this method when using a slice of [`char`]s. + /// Some users may expect that a slice of chars will behave similarly to a `&str` with this method. + /// That is not currently the case. When you pass a slice of [`char`]s to this method, it will return true + /// if any of the [`char`]s in the slice is the first [`char`] of this string slice. It does not work for + /// sequentially comparing a slice of [`char`]s to a string slice. See the second example below. + /// /// [`char`]: prim@char /// [pattern]: self::pattern /// @@ -1171,6 +1177,14 @@ pub fn contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool { /// assert!(bananas.starts_with("bana")); /// assert!(!bananas.starts_with("nana")); /// ``` + /// + /// ``` + /// let bananas = "bananas"; + /// + /// // Note that both of these assert successfully. + /// assert!(bananas.starts_with(&['b', 'a', 'n', 'a'])); + /// assert!(bananas.starts_with(&['a', 'b', 'c', 'd'])); + /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn starts_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool { pat.is_prefix_of(self) From 8ff1994ec06a0bc4d8f80ecbefe981013b902a9b Mon Sep 17 00:00:00 2001 From: Venus Xeon-Blonde Date: Wed, 7 Feb 2024 23:37:34 -0500 Subject: [PATCH 2/3] Fix whitespace issues that tidy caught --- library/core/src/str/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index b90f34360f6..597d924469c 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -1161,7 +1161,7 @@ pub fn contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool { /// function or closure that determines if a character matches. /// /// Note that there is a footgun to this method when using a slice of [`char`]s. - /// Some users may expect that a slice of chars will behave similarly to a `&str` with this method. + /// Some users may expect that a slice of chars will behave similarly to a `&str` with this method. /// That is not currently the case. When you pass a slice of [`char`]s to this method, it will return true /// if any of the [`char`]s in the slice is the first [`char`] of this string slice. It does not work for /// sequentially comparing a slice of [`char`]s to a string slice. See the second example below. @@ -1177,11 +1177,11 @@ pub fn contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool { /// assert!(bananas.starts_with("bana")); /// assert!(!bananas.starts_with("nana")); /// ``` - /// + /// /// ``` /// let bananas = "bananas"; - /// - /// // Note that both of these assert successfully. + /// + /// // Note that both of these assert successfully. /// assert!(bananas.starts_with(&['b', 'a', 'n', 'a'])); /// assert!(bananas.starts_with(&['a', 'b', 'c', 'd'])); /// ``` From d7263d7aada418eb22d0560388379a2e3658e211 Mon Sep 17 00:00:00 2001 From: Venus Xeon-Blonde Date: Fri, 9 Feb 2024 22:24:57 -0500 Subject: [PATCH 3/3] Change wording --- library/core/src/str/mod.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 597d924469c..e11d13f8bed 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -1157,14 +1157,13 @@ pub fn contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool { /// /// Returns `false` if it does not. /// - /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a - /// function or closure that determines if a character matches. + /// The [pattern] can be a `&str`, in which case this function will return true if + /// the `&str` is a prefix of this string slice. /// - /// Note that there is a footgun to this method when using a slice of [`char`]s. - /// Some users may expect that a slice of chars will behave similarly to a `&str` with this method. - /// That is not currently the case. When you pass a slice of [`char`]s to this method, it will return true - /// if any of the [`char`]s in the slice is the first [`char`] of this string slice. It does not work for - /// sequentially comparing a slice of [`char`]s to a string slice. See the second example below. + /// The [pattern] can also be a [`char`], a slice of [`char`]s, or a + /// function or closure that determines if a character matches. + /// These will only be checked against the first character of this string slice. + /// Look at the second example below regarding behavior for slices of [`char`]s. /// /// [`char`]: prim@char /// [pattern]: self::pattern