Can't call unwrap on associated type

Hello,
I am trying to implement 'generic' string parser. Here is my code:

pub trait Solution {
type OutputPartOne: Display;
type OutputPartTwo: Display;
type Input: std::str::FromStr;

fn read_input(filename: &Path) -> Vec<Self::Input> {
    let f = File::open(filename)
        .expect(&format!("Could not open file at path {:?}!", filename));
    let f = BufReader::new(f);

    f.lines().filter_map(|l| l.ok()).map(|l| l.parse::<Self::Input>().unwrap()).collect()
}

Problem says:
method 'unwrap' not found in std::result::Result<<Self as puzzles::solution::Solution>::Input, <<Self as puzzles::solution::Solution>::Input as std::str::FromStr>::Err> | = note: the method unwrap exists but the following trait bounds were not satisfied: <<Self as puzzles::solution::Solution>::Input as std::str::FromStr>::Err: std::fmt::Debug

It seems like my 'Input' is not allowed to call 'unwrap' on. Could you help me with this one? The goal is to provide function parsing each line as integers, floats or just Strings (Depending on Input type).

thanks!

Result<T, E> is only unwrap-pable if the error type impls Debug, since the panic message wants to print the error representation. You thus need to add the suggested trait bound in the function signature (<Self::Input as FromStr>::Err: Debug).

You can deduce all of this from the documentation.

Ok, I understand the problem but technically, where/how should I add this to my code?
Thanks for help

that is,

fn read_input(filename: &Path) -> Vec<Self::Input>
   where <Self::Input as FromStr>::Err: Debug
{
    ...
}
1 Like

ah ok, didn't know it was about 'where' part. Thanks a lot!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.