【发布时间】:2020-08-23 08:12:36
【问题描述】:
考虑以下最小示例:
fn foo<T>(arg: T, f: impl FnOnce(i32) -> i32) -> Vec<T> {
let mut x: Vec<T> = Vec::new();
x.push(arg);
println!("{:?}", f(5));
x
}
fn main() {
println!("{:?}", foo::<u32>(5, |i_arg| { i_arg + 5 }));
}
因为使用了impl Trait而无法编译:
error[E0632]: cannot provide explicit generic arguments when `impl Trait` is used in argument position
--> src/main.rs:10:28
|
10 | println!("{:?}", foo::<u32>(5, |i_arg| { i_arg + 5 }));
| ^^^ explicit generic argument not allowed
error: aborting due to previous error
error: could not compile `generic_functions`.
尽管在另一个参数中使用了impl Trait,我是否有可能为T 指定类型?我在一个代码库中工作,我需要调用一个使用impl Trait 的泛型函数,但我需要指定其他泛型类型。
【问题讨论】:
标签: generics rust numbers closures traits