I tried to write an average function, but so far failed to convince
the type checker.
fn average<T:Int>(values:&[T]) -> T {
let count = values.len();
let mut sum:T;
for v in values.iter() {
sum = sum.add(v);
}
return sum / count;
}
error: mismatched types: expected `T` but found `uint` (expected type
parameter but found uint)
The problem is that sum is the generic type T, but count is uint due
to the definition of the len function. Casting "count as T" should
work, i thought, but rustc seems to have another opinion?
--
Andreas Zwinkau
work email: [email protected]
private email: [email protected]
homepage: http://beza1e1.tuxen.de
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev