#string #pattern #traits #polyfill #depend #string-matching #white-space

stable_string_patterns_method

Stable polyfill to be able to depends on a trait that mimics str::pattern::Pattern on stable Rust

1 unstable release

Uses old Rust 2015

0.0.1 Jan 21, 2026

#31 in #polyfill

MIT license

44KB
1K SLoC

This crates allows writing functions taking any type implementing std::str::pattern::Pattern on stable Rust.

This works by using a "proxy" trait, [Searchable]. Instead of depending on the unstable std::str::pattern::Pattern, you must use [IntoSearchable] as a bound in your code. This in turns lets you use the functions defined in [StrPatternExt], which mimc the ones defined on [str], except that they end with an underscore.

On top of the implementors of std::str::pattern::Pattern, the [WhiteSpace] type is also provided, with also implements [IntoSearchable].

Example

fn starts_and_ends_with(haystack: &str, p: impl IntoSearchable + Clone) -> bool {
    haystack.starts_with_(p.clone()) && haystack.ends_with_(p)
}

assert!(starts_and_ends_with("aa bb aa", "aa"));
assert!(starts_and_ends_with("aa bb aa", 'a'));
assert!(starts_and_ends_with("aa bb cc", ['a','c']));
assert!(starts_and_ends_with("\t bb  \n", WhiteSpace));

Stable polyfill to be able to depends on a trait that mimics str::pattern::Pattern on stable Rust.

fn starts_and_ends_with(haystack: &str, p: impl IntoSearchable + Clone) -> bool {
    haystack.starts_with_(p.clone()) && haystack.ends_with_(p)
}

assert!(starts_and_ends_with("aa bb aa", "aa"));
assert!(starts_and_ends_with("aa bb aa", 'a'));
assert!(starts_and_ends_with("aa bb cc", ['a','c']));
assert!(starts_and_ends_with("\t bb  \n", WhiteSpace));

No runtime deps

Features