我有一个简单的通用函数:
fn add_five<T: Into<i32>>(t: T) -> i32 {
5_i32 + t.into()
}
我想用From
trait 而不是Into
trait 来表达这一点,但是我try 了重构:
fn add_five<T, i32: From<T>>(t: T) -> i32 {
5_i32 + <i32 as From<T>>::from(t)
}
抛出以下编译错误:
error[E0277]: cannot add `i32` to `i32`
--> src/main.rs:24:11
|
| 5_i32 + <i32 as From<T>>::from(t)
| ^ no implementation for `i32 + i32`
|
= help: the trait `Add<i32>` is not implemented for `i32`
这非常令人困惑,因为标准库中确实有impl Add<i32> for i32
个,那么真正的问题是什么?