简化函数声明

我想简化以下函数的声明:

use regex::Regex;

fn oper<'a, F>(regex: &str, op: F) -> (Regex, Box<dyn Fn(i32, i32) -> i32 + 'a>)
where F: Fn(i32, i32) -> i32 + 'a
{
    (Regex::new(regex).unwrap(), Box::new(op))
}

I tried to replace the Fn trait by F in the return value, but it raises an error:

fn oper<'a, F>(regex: &str, op: F) -> (Regex, Box<dyn F>)
where F: Fn(i32, i32) -> i32 + 'a
{
    (Regex::new(regex).unwrap(), Box::new(op))
}
error[E0404]: expected trait, found type parameter `F`
  --> src/lib.rs:5:55
   |
5  |   fn oper<'a, F>(regex: &str, op: F) -> (Regex, Box<dyn F>)
   |                                                         ^ help: a trait with a similar name exists: `Fn`

error: aborting due to previous error

How can simplify this declaration to avoid duplication of Fn(i32, i32) -> i32 + 'a?

评论