pub trait Like<Rhs = Self> {
// Required method
fn like(&self, other: &Rhs) -> bool;
}
Expand description
A trait for pattern matching, similar to PartialEq
but for flexible matching.
The Like
trait enables custom pattern matching logic beyond simple equality.
It’s primarily used with the =~
operator in assert_struct!
macro to support
regex patterns, custom matching logic, and other pattern-based comparisons.
§Examples
§Basic String Pattern Matching
use assert_struct::Like;
// Using Like trait directly
let text = "hello@example.com";
assert!(text.like(&r".*@example\.com"));
§Custom Implementation
use assert_struct::Like;
struct EmailAddress(String);
struct DomainPattern {
domain: String,
}
impl Like<DomainPattern> for EmailAddress {
fn like(&self, pattern: &DomainPattern) -> bool {
self.0.ends_with(&format!("@{}", pattern.domain))
}
}
let email = EmailAddress("user@example.com".to_string());
let pattern = DomainPattern { domain: "example.com".to_string() };
assert!(email.like(&pattern));
Required Methods§
Implementations on Foreign Types§
Source§impl Like for String
Implementation of Like for String with String patterns (interpreted as regex)
impl Like for String
Implementation of Like for String with String patterns (interpreted as regex)
Source§impl Like<&str> for &str
Implementation of Like for &str with &str patterns (interpreted as regex)
impl Like<&str> for &str
Implementation of Like for &str with &str patterns (interpreted as regex)
Source§impl Like<&str> for String
Implementation of Like for String with &str patterns (interpreted as regex)
impl Like<&str> for String
Implementation of Like for String with &str patterns (interpreted as regex)
Source§impl Like<String> for &str
Implementation of Like for &str with String patterns (interpreted as regex)
impl Like<String> for &str
Implementation of Like for &str with String patterns (interpreted as regex)