<分区>
我是 Rust 的新手,我正在尝试编写自己的简单通用函数。
fn templ_sum<T>(x : T, y : T) -> T
where T : std::ops::Add
{
let res : T = x + y;
res
}
fn main()
{
let x : f32 = 1.0;
let y : f32 = 2.0;
let z = templ_sum(x, y);
println!("{}", z);
}
但是编译失败并提示信息
error: mismatched types: expected
T
, found<T as core::ops::Add>::Output
(expected type parameter, found associated type) [E0308] main.rs:12 let res : T = x + y;
我有点懵。谁能向我解释我做错了什么?
rustc --version: rustc 1.2.0 (082e47636 2015-08-03)