In this code
pub struct Ytz<K, V> {
table: std::collections::HashMap<K, V>,
}
impl<K, V> Ytz<K, V>
where
K: std::cmp::Eq + std::hash::Hash + std::convert::Into<V>,
V: std::ops::AddAssign + std::cmp::PartialOrd,
{
pub fn new() -> Self {
Self {
table: std::collections::HashMap::<K, V>::new(),
}
}
pub fn add(&mut self, item: &K) {
if self.table.contains_key(item) {
let to_add: V = *item.into(); // <--- here
*self.table.get_mut(item).unwrap() += to_add;
} else {
self.table.insert(*item, *item.into());
}
}
pub fn largest(&self) -> V {
let mut result = self.table.values().nth(0).unwrap();
for v in self.table.values() {
if result < v.into() {
result = v.into();
}
}
*result
}
}
error[E0282]: type annotations needed
--> src/main.rs:18:35
|
18 | let to_add: V = *item.into();
| ^^^^ cannot infer type for `T`
|
= note: type must be known at this point
the type annotation is already given. Why can't the compiler infer the type?