about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-02-10 13:12:29 +0100
committerGitHub <noreply@github.com>2024-02-10 13:12:29 +0100
commit2eda0c7b2e4ea5cfbb99778fd9266388a5096292 (patch)
tree7d5fb42b33f3bda6b861a67814542afbc50a37ae
parente11e4446daf09d3278c89a2cadf8865533c7fc46 (diff)
parentd7263d7aada418eb22d0560388379a2e3658e211 (diff)
downloadrust-2eda0c7b2e4ea5cfbb99778fd9266388a5096292.tar.gz
rust-2eda0c7b2e4ea5cfbb99778fd9266388a5096292.zip
Rollup merge of #120764 - Alfriadox:master, r=m-ou-se
Add documentation on `str::starts_with`

Add documentation about a current footgun of `str::starts_with`
-rw-r--r--library/core/src/str/mod.rs15
1 files changed, 14 insertions, 1 deletions
diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs
index ecc0613d7b9..e11d13f8bed 100644
--- a/library/core/src/str/mod.rs
+++ b/library/core/src/str/mod.rs
@@ -1157,8 +1157,13 @@ impl str {
     ///
     /// Returns `false` if it does not.
     ///
-    /// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
+    /// The [pattern] can be a `&str`, in which case this function will return true if
+    /// the `&str` is a prefix of this string slice.
+    ///
+    /// 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
@@ -1171,6 +1176,14 @@ impl str {
     /// 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)