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`
This commit is contained in:
Matthias Krüger 2024-02-10 13:12:29 +01:00 committed by GitHub
commit 2eda0c7b2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1157,8 +1157,13 @@ pub fn contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
/// ///
/// Returns `false` if it does not. /// 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. /// 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 /// [`char`]: prim@char
/// [pattern]: self::pattern /// [pattern]: self::pattern
@ -1171,6 +1176,14 @@ pub fn contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
/// assert!(bananas.starts_with("bana")); /// assert!(bananas.starts_with("bana"));
/// assert!(!bananas.starts_with("nana")); /// 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")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn starts_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool { pub fn starts_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
pat.is_prefix_of(self) pat.is_prefix_of(self)