Skip to main content

Like

Trait Like 

Source
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§

Source

fn like(&self, other: &Rhs) -> bool

Returns true if self matches the pattern other.

§Examples
use assert_struct::Like;

let s = "test123";
assert!(s.like(&r"\w+\d+"));

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Like for String

Implementation of Like for String with String patterns (interpreted as regex)

Source§

fn like(&self, pattern: &String) -> bool

Source§

impl Like<&str> for &str

Implementation of Like for &str with &str patterns (interpreted as regex)

Source§

fn like(&self, pattern: &&str) -> bool

Source§

impl Like<&str> for String

Implementation of Like for String with &str patterns (interpreted as regex)

Source§

fn like(&self, pattern: &&str) -> bool

Source§

impl Like<Regex> for &str

Implementation of Like for &str with pre-compiled Regex

Source§

fn like(&self, pattern: &Regex) -> bool

Source§

impl Like<Regex> for String

Implementation of Like for String with pre-compiled Regex

Source§

fn like(&self, pattern: &Regex) -> bool

Source§

impl Like<String> for &str

Implementation of Like for &str with String patterns (interpreted as regex)

Source§

fn like(&self, pattern: &String) -> bool

Implementors§