Skip to content

Commit 4ecc5c5

Browse files
authored
Add documentation on str::starts_with
Add documentation about a current footgun of `str::starts_with`
1 parent 00d45b8 commit 4ecc5c5

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Diff for: core/src/str/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,12 @@ impl str {
11601160
/// The [pattern] can be a `&str`, [`char`], a slice of [`char`]s, or a
11611161
/// function or closure that determines if a character matches.
11621162
///
1163+
/// Note that there is a footgun to this method when using a slice of [`char`]s.
1164+
/// Some users may expect that a slice of chars will behave similarly to a `&str` with this method.
1165+
/// That is not currently the case. When you pass a slice of [`char`]s to this method, it will return true
1166+
/// if any of the [`char`]s in the slice is the first [`char`] of this string slice. It does not work for
1167+
/// sequentially comparing a slice of [`char`]s to a string slice. See the second example below.
1168+
///
11631169
/// [`char`]: prim@char
11641170
/// [pattern]: self::pattern
11651171
///
@@ -1171,6 +1177,14 @@ impl str {
11711177
/// assert!(bananas.starts_with("bana"));
11721178
/// assert!(!bananas.starts_with("nana"));
11731179
/// ```
1180+
///
1181+
/// ```
1182+
/// let bananas = "bananas";
1183+
///
1184+
/// // Note that both of these assert successfully.
1185+
/// assert!(bananas.starts_with(&['b', 'a', 'n', 'a']));
1186+
/// assert!(bananas.starts_with(&['a', 'b', 'c', 'd']));
1187+
/// ```
11741188
#[stable(feature = "rust1", since = "1.0.0")]
11751189
pub fn starts_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
11761190
pat.is_prefix_of(self)

0 commit comments

Comments
 (0)