Ich versuche, eine parametrisierte Funktion if_found_update
, die einen Wert in der Hash-Updates zu schreiben, wenn es vorhanden ist:Deref Zwang mit Generika
use std::collections::HashMap;
fn if_found_update<K, V>(data: &mut HashMap<K, V>, k: &K, v: &V, f: &Fn(&V, &V) -> V) -> bool
where K: std::cmp::Eq,
K: std::hash::Hash
{
if let Some(e) = data.get_mut(k) {
*e = f(e, v);
return true;
}
false
}
fn main() {
let mut h: HashMap<String, i64> = HashMap::new();
h.insert("A".to_string(), 0);
let one = 1 as i64;
fn update(e1: &i64, e2: &i64) -> i64 {
e1 + e2
};
let k: &str = &"A".to_string();
println!("{}",
if_found_update(&mut h, &"A".to_string(), &one, &update)); // works
println!("{}", if_found_update(&mut h, k, &one, &update)); // fails to compile
}
if_found_update(&mut h, &"A".to_string(), &one, &update);
funktioniert gut, aber if_found_update(&mut h, k, &one, &update)
irgendwie kompilieren:
error[E0308]: mismatched types
--> src/main.rs:24:44
|
24 | println!("{}", if_found_update(&mut h, k, &one, &update)); // fails to compile
| ^expected struct `std::string::String`, found str
|
= note: expected type `&std::string::String`
= note: found type `&str`
Ich denke es ist, weil es für die entsprechende deref-Zwang fehlschlägt. Gibt es eine Möglichkeit, so etwas zum Laufen zu bringen?
Vielen Dank! Ich habe 'Borrow' versucht, aber ich habe es nicht richtig benutzt. – divbyzero