Help Understanding Type Annotation Error

I'm fairly new to Rust, and I'm having trouble understanding the error message that I'm getting in relation to some trait bounding issues.

I am using the trie_rs crate to build a trie of a custom enum of English letters that I have defined (in an attempt to reduce space usage given that my use case doesn't require the full UTF-8 charset). Here is the definition of the enum

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
/// simplify storage as our grammar consists of only upper case english letters
/// as opposed to all unicode values.
pub enum Letter {
    A,
    B,
    C,
    D,
    E,
    F,
    G,
    H,
    I,
    J,
    K,
    L,
    M,
    N,
    O,
    P,
    Q,
    R,
    S,
    T,
    U,
    V,
    W,
    X,
    Y,
    Z,
}

I am trying to verify that my particular implementation is working correctly by searching for matches for a specifix prefix in the trie as seen below

    let trie: Trie<Letter> = create_trie(file_path);
    const TEST_STR: &str = "APP";
    if let Ok(translated_word: Vec<Letter>) = translate_word(&String::from(TEST_STR)){
	let results = trie.predictive_search(translated_word);
	for result in results.into_iter() {
	    println!("Found result {}", result);
	}

However, this does not compile with the following error message:

error[E0283]: type annotations needed for `trie_rs::iter::Keys<SearchIter<'_, Letter, (), _, _>>`
  --> src/main.rs:27:6
   |
27 |     let results = trie.predictive_search(translated_word);
   |         ^^^^^^^        ----------------- type must be known at this point
   |
   = note: cannot satisfy `_: TryFromIterator<Letter, _>`
   = help: the trait `TryFromIterator<u8, StringCollect>` is implemented for `String`

The trait bounds required for predictive_search can be seen in the docs (apologies that I can't link directly - new users are only allowed 2 links per post). I would expect the "blanket" implementation of TryFromIterator seen here to match here. When I try to explicitly annotate that, I get a variety of trait errors, so I think I'm doing something wrong.

Is there a reason that the blanket implementation is not matching? For lack of better phrasing, how should I go about doing what I'm trying to do (get a search working for a trie built from my custom enum).

1 Like

This is saying you need to specify a type annotation for the results variable, which is what we see in the examples in the API doc,

let results_in_u8s: Vec<Vec<u8>> = trie.predictive_search("すし").collect();
let results_in_str: Vec<String> = trie.predictive_search("すし").collect();
//                ^^^^^^^^^^^^^

Have you tried that?

4 Likes

Thank you so much. I had overlooked that aspect of the examples in the docs, and it makes complete sense in retrospect that not knowing the type of results would preclude the compiler from determining the appropriate type for the TryFromIterator (which is what I had been trying to type annotate without success).

For completeness's sake, the solution was changing line 27 to read let results: Vec<Vec<Letter>> = trie.predictive_search(translated_word).collect();.

1 Like