Matcher

Struct Matcher 

Source
pub struct Matcher<'a> { /* private fields */ }

Implementations§

Source§

impl<'a> Matcher<'a>

Source

pub fn new(items: &[&'a str]) -> Self

Expect the items to be pre-formatted (lowercase)

Examples found in repository?
examples/autocomplete.rs (line 31)
4fn main() {
5    println!("=== QuickMatch Autocomplete Demo ===\n");
6
7    let products = vec![
8        // "Apple iPhone 15 Pro",
9        // "Apple MacBook Pro 16",
10        // "Apple AirPods Pro",
11        // "Samsung Galaxy S24",
12        // "Samsung Galaxy Tab",
13        // "Sony PlayStation 5",
14        "Sony WH-1000XM5 Headphones",
15        // "Microsoft Surface Pro",
16        // "Microsoft Xbox Series X",
17        // "Dell XPS 13 Laptop",
18        // "Dell UltraSharp Monitor",
19        // "Logitech MX Master Mouse",
20        // "Logitech Mechanical Keyboard",
21        // "Canon EOS R5 Camera",
22        // "Nikon Z9 Camera",
23        // "GoPro Hero 12",
24    ]
25    .into_iter()
26    .map(|s| s.to_lowercase())
27    .collect::<Vec<_>>();
28
29    let products_ref = products.iter().map(|s| s.as_str()).collect::<Vec<_>>();
30
31    let matcher = Matcher::new(&products_ref);
32
33    println!("Type to search (press Ctrl+C to exit):");
34    println!("Try: 'apple', 'pro', 'laptop', 'headphones', etc.\n");
35
36    loop {
37        print!("> ");
38        io::stdout().flush().unwrap();
39
40        let mut input = String::new();
41        io::stdin().read_line(&mut input).unwrap();
42        let query = input.trim();
43
44        if query.is_empty() {
45            continue;
46        }
47
48        let results = matcher.matches(query, usize::MAX);
49
50        if results.is_empty() {
51            println!("  No matches found\n");
52        } else {
53            println!("  {} result(s):", results.len());
54            for result in results {
55                println!("    • {}", result);
56            }
57            println!();
58        }
59    }
60}
Source

pub fn matches(&self, query: &str, limit: usize) -> Vec<&'a str>

Examples found in repository?
examples/autocomplete.rs (line 48)
4fn main() {
5    println!("=== QuickMatch Autocomplete Demo ===\n");
6
7    let products = vec![
8        // "Apple iPhone 15 Pro",
9        // "Apple MacBook Pro 16",
10        // "Apple AirPods Pro",
11        // "Samsung Galaxy S24",
12        // "Samsung Galaxy Tab",
13        // "Sony PlayStation 5",
14        "Sony WH-1000XM5 Headphones",
15        // "Microsoft Surface Pro",
16        // "Microsoft Xbox Series X",
17        // "Dell XPS 13 Laptop",
18        // "Dell UltraSharp Monitor",
19        // "Logitech MX Master Mouse",
20        // "Logitech Mechanical Keyboard",
21        // "Canon EOS R5 Camera",
22        // "Nikon Z9 Camera",
23        // "GoPro Hero 12",
24    ]
25    .into_iter()
26    .map(|s| s.to_lowercase())
27    .collect::<Vec<_>>();
28
29    let products_ref = products.iter().map(|s| s.as_str()).collect::<Vec<_>>();
30
31    let matcher = Matcher::new(&products_ref);
32
33    println!("Type to search (press Ctrl+C to exit):");
34    println!("Try: 'apple', 'pro', 'laptop', 'headphones', etc.\n");
35
36    loop {
37        print!("> ");
38        io::stdout().flush().unwrap();
39
40        let mut input = String::new();
41        io::stdin().read_line(&mut input).unwrap();
42        let query = input.trim();
43
44        if query.is_empty() {
45            continue;
46        }
47
48        let results = matcher.matches(query, usize::MAX);
49
50        if results.is_empty() {
51            println!("  No matches found\n");
52        } else {
53            println!("  {} result(s):", results.len());
54            for result in results {
55                println!("    • {}", result);
56            }
57            println!();
58        }
59    }
60}

Trait Implementations§

Source§

impl<'a> Send for Matcher<'a>

Source§

impl<'a> Sync for Matcher<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Matcher<'a>

§

impl<'a> RefUnwindSafe for Matcher<'a>

§

impl<'a> Unpin for Matcher<'a>

§

impl<'a> UnwindSafe for Matcher<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.