Can not return result from a function

I'm trying to make simple example with tera, where tera.render(template_name, context) returns Result<String, Error>

So, I wrote the below:

fn format_email(tera: &Tera, name: &str, years_as_client: u32, sender: &str) -> Result<String, Error> {
    let normalized_duration = if years_as_client == 1 {
        String::from("1 year")
    } else {
        format!("{} years", years_as_client)
    };

    let mut context = Context::new();
    context.add("name", &name);
    context.add("years_as_client", &normalized_duration);
    context.add("sender", &sender);

    match tera.render("email.txt", context) {
        Ok(template) => return Ok(template),
        Err(e) => return Err(e),
    }
}

But I got the below error for the returned type of the function:

warning: trait objects without an explicit `dyn` are deprecated
  --> src/main.rs:83:96
   |
83 | fn format_email(tera: &Tera, name: &str, years_as_client: u32, sender: &str) -> Result<String, Error> {
   |                                                                                                ^^^^^ help: use `dyn`: `dyn Error`
   |
   = note: #[warn(bare_trait_objects)] on by default

error[E0107]: wrong number of type arguments: expected 1, found 2
  --> src/main.rs:83:96
   |
83 | fn format_email(tera: &Tera, name: &str, years_as_client: u32, sender: &str) -> Result<String, Error> {
   |                                                                                                ^^^^^ unexpected type argument

And in the IDEA I get this error:

Wrong number of type parameters: expected 1, found 2 [E0244]

Error is not a type, just like Debug and Eq (equality) is not a type. You can't make it, you can't return it.

You can only return something else that happens to implement the Error interface, which is Box<dyn std::error::Error>.

if return Err(e) doesn't work, try Err(e)?, because ? does conversions.

BTW, watch out for things named Result and Error that come from 3rd party libraries, rather than the standard library. If you import things with use foo::*, you may accidentally replace standard result/error with library's own. That may be the source of "Wrong number of type parameters" errors, because libraries have their own versions with a different number of parameters. If in doubt, write std::result::Result and std::error::Error.

1 Like

Thanks,

  1. You are correct about the Result I found that by mistake I'm using the one defined in the tera instead of std
  2. It worked now using Box<dyn Error> and Err(e)?, below the final code worked with me:
fn format_email(..) -> Result<String, Box<dyn Error>> {
        match tera.render("email.txt", context) {
        Ok(template) => Ok(template),
        Err(e) => Err(e)?,
    }
}

I was able to do it also using alias, as:

type OryxResult<T> = std::result::Result<T, Box<dyn error::Error>>;

fn format_email(..) -> OryxResult<String> {
        match tera.render("email.txt", context) {
        Ok(template) => Ok(template),
        Err(e) => Err(e)?,
    }
}
1 Like