Po prostu uczę się rdzy i pracuję nad zadaniem easy/r/dailyprogrammer. Oto niektóre kodu:Dlaczego nie mogę zniszczyć tej krotki podczas iteracji nad HashMap?
type ToDoList = HashMap<String, bool>;
fn print(list: &ToDoList) {
let mut max_len: usize = 0;
for (item, _) in list.iter() {
max_len = max(max_len, item.len());
}
let end = format!("+---{}-+",
iter::repeat("-").take(max_len).collect::<String>());
println!("{}", end);
for (item, done) in list.iter() {
let line = format!("| {0} {1}{2} |",
if done {"☑"} else {"☐"},
item,
iter::repeat("-")
.take(max_len - item.len())
.collect::<String>()
);
println!("{:?}", (item, done));
}
println!("{}", end);
}
dostaję ten błąd z rustc:
error: type mismatch resolving `<std::collections::hash::map::Iter<'_,
collections::string::String, bool> as core::iter::Iterator>::Item ==
(_, bool)`: expected &-ptr,
found bool [E0271]
todolist.rs:19 for (item, done) in list.iter() {
todolist.rs:20 let line = format!("| {0} {1}{2} |",
todolist.rs:21 if done {"☑"} else {"☐"},
todolist.rs:22 item,
todolist.rs:23 iter::repeat("-")
todolist.rs:24 .take(max_len - item.len())
...
todolist.rs:24:21: 24:31 error: the type of this value must be known in this context
todolist.rs:24 .take(max_len - item.len())
^~~~~~~~~~ note: in expansion of format_args! <std macros>:2:26: 2:57 note: expansion site <std
macros>:1:1: 2:61 note: in expansion of format!
todolist.rs:20:14: 26:4 note: expansion site error: aborting due to 2 previous errors
Wydaje się, że oba te związane są z tej samej kwestii, że jakoś wywołującego list.iter()
próbuje dać mi krotki z (_, String, bool)
zamiast tylko (String, bool)
. Dlaczego tak się dzieje?