about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2020-06-13 16:47:40 +0200
committerGitHub <noreply@github.com>2020-06-13 16:47:40 +0200
commit2cc267245dc1df5920190e7b3555a13bfacb11c5 (patch)
tree15092a1a447669651d4d1d4656c5f2f95b01495d /src/libcore
parent1fb612bd15bb3ef098fd24c20d0727de573b4410 (diff)
parent4548eb8fcfa0a3d5dfd2766ee12736f67aca0234 (diff)
downloadrust-2cc267245dc1df5920190e7b3555a13bfacb11c5.tar.gz
rust-2cc267245dc1df5920190e7b3555a13bfacb11c5.zip
Rollup merge of #72932 - poliorcetics:pattern-contains-behaviour, r=hanna-kruppe
Clarify the behaviour of Pattern when used with methods like str::contains

Fixes #45507.

I used the previous work by @Emerentius (thanks !), added a paragraph and checked the links (they work for me but I'm not against someone else checking them too).
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/str/pattern.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/libcore/str/pattern.rs b/src/libcore/str/pattern.rs
index 1a2b612b2f9..263d6b5efdf 100644
--- a/src/libcore/str/pattern.rs
+++ b/src/libcore/str/pattern.rs
@@ -60,6 +60,43 @@ use crate::slice::memchr;
 /// The trait itself acts as a builder for an associated
 /// `Searcher` type, which does the actual work of finding
 /// occurrences of the pattern in a string.
+///
+/// Depending on the type of the pattern, the behaviour of methods like
+/// [`str::find`] and [`str::contains`] can change. The table below describes
+/// some of those behaviours.
+///
+/// | Pattern type             | Match condition                           |
+/// |--------------------------|-------------------------------------------|
+/// | `&str`                   | is substring                              |
+/// | `char`                   | is contained in string                    |
+/// | `&[char]                 | any char in slice is contained in string  |
+/// | `F: FnMut(char) -> bool` | `F` returns `true` for a char in string   |
+/// | `&&str`                  | is substring                              |
+/// | `&String`                | is substring                              |
+///
+/// # Examples
+/// ```
+/// // &str
+/// assert_eq!("abaaa".find("ba"), Some(1));
+/// assert_eq!("abaaa".find("bac"), None);
+///
+/// // char
+/// assert_eq!("abaaa".find('a'), Some(0));
+/// assert_eq!("abaaa".find('b'), Some(1));
+/// assert_eq!("abaaa".find('c'), None);
+///
+/// // &[char]
+/// assert_eq!("ab".find(&['b', 'a'][..]), Some(0));
+/// assert_eq!("abaaa".find(&['a', 'z'][..]), Some(0));
+/// assert_eq!("abaaa".find(&['c', 'd'][..]), None);
+///
+/// // FnMut(char) -> bool
+/// assert_eq!("abcdef_z".find(|ch| ch > 'd' && ch < 'y'), Some(4));
+/// assert_eq!("abcddd_z".find(|ch| ch > 'd' && ch < 'y'), None);
+/// ```
+///
+/// [`str::find`]: ../../../std/primitive.str.html#method.find
+/// [`str::contains`]: ../../../std/primitive.str.html#method.contains
 pub trait Pattern<'a>: Sized {
     /// Associated searcher for this pattern
     type Searcher: Searcher<'a>;