Function lib::struct_rank

source ·
pub fn struct_rank<'a, T: Clone>(
    query: &str,
    subjects: Vec<T>,
    accessor: fn(_: &T) -> String
) -> Vec<(f64, T)>
Expand description

Ranks a list of structured objects based on their similarity to a query string using a custom accessor function.

§Arguments

  • query - A reference to the query string.
  • subjects - A vector of structured objects to be ranked.
  • accessor - A function that takes a reference to a structured object and returns a string for comparison.

§Returns

  • A vector of tuples containing the similarity score and the corresponding structured object, sorted by score in descending order.

§Examples

struct Person {
    name: String,
}

let query = "Ad";
let subjects = vec![
    Person { name: "Ad_Fields".to_string() },
    Person { name: "Users".to_string() },
    Person { name: "Aged_groups".to_string() },
];
let result = struct_rank(query, subjects, |p| p.name.clone());
assert_eq!(result[0].0, 2.0);
assert_eq!(result[0].1.name, "Ad_Fields");