Skip to main content

nearest_match

Function nearest_match 

Source
pub(crate) fn nearest_match<'a, I>(
    unknown: &str,
    candidates: I,
) -> Option<&'a str>
where I: IntoIterator<Item = &'a str>,
Expand description

The single candidate that is a reasonable near-miss for unknown, if any.

“Reasonable” is deliberately conservative — a wrong guess is worse than no guess. The rule mirrors the citation-key heuristic in [crate::bibliography]; nearest_label in crate::resolve shares the length floor and distance bound but keeps its own deterministic lexicographic tie-break instead of the tie rule below:

  • names shorter than three bytes get no suggestion (a one-edit guess on a one- or two-byte name is noise, not help);
  • the edit distance must be within unknown.len() / 3: rustc’s “did you mean” style bound. With the length floor that bound is always at least 1, admitting textext (distance 1, bound 1) while rejecting wholly unrelated names;
  • two candidates tied at the best distance mean the intent is ambiguous, so nothing is suggested at all.