utilz_rs/
str_utils.rs

1/// Extra methods for string slices (`&str`).
2pub trait StrUtils {
3    /// Returns `true` if all strings in the iterator exist in the main string.
4    fn contains_all<'a, I>(&self, parts: I) -> bool
5    where
6        I: IntoIterator<Item = &'a str>;
7
8    /// Returns `true` if any of the strings in the iterator exist in the main string.
9    fn contains_any<'a, I>(&self, parts: I) -> bool
10    where
11        I: IntoIterator<Item = &'a str>;
12
13    /// Returns a new string with the first letter capitalized.
14    fn to_title_case(&self) -> String;
15}
16
17impl StrUtils for str {
18    fn contains_all<'a, I>(&self, parts: I) -> bool
19    where
20        I: IntoIterator<Item = &'a str>,
21    {
22        parts.into_iter().all(|part| self.contains(part))
23    }
24    fn contains_any<'a, I>(&self, parts: I) -> bool
25    where
26        I: IntoIterator<Item = &'a str>,
27    {
28        parts.into_iter().any(|part| self.contains(part))
29    }
30    fn to_title_case(&self) -> String {
31        let mut chars = self.chars();
32        match chars.next() {
33            None => String::new(),
34            Some(f) => f.to_uppercase().collect::<String>() + chars.as_str(),
35        }
36    }
37}