Why I am getting `this generic parameter must be used with a generic type parameter` error for following code?

  Kiến thức lập trình

sorry for the noob question but I am not able to figure out why following code is giving me error this generic parameter must be used with a generic type parameter

use anyhow::Result;
use std::path::Path;

fn test_file(path: impl AsRef<Path>, filename: &str) -> Result<Option<impl AsRef<Path>>> {
    ...
    if path.join(filename).exist() {
        Ok(Some(path))
    } else if current_directory == path {
        Ok(None)
    } else {
        test_file(path.parent().context("{filename} not found")?, filename)
    }
}

As you can see this is a recursive function and I am getting error in else condition while passing path for parent directory, but surprisingly if I change the return type to Result<bool> with following function body then compiler passes without any error.

use anyhow::Result;
use std::path::Path;

fn test_file(path: impl AsRef<Path>, filename: &str) -> Result<bool> {
    ...
    if path.join(filename).exist() {
        Ok(true)
    } else if current_directory == path {
        Ok(false)
    } else {
        test_file(path.parent().context("{filename} not found")?, filename)
    }
}

I am not able to figure out what I am doing wrong here, can someone please help?

Thanks in advance 🙂

1

LEAVE A COMMENT