I just study rust and work on the easy / r / dailyprogrammer task. Here is the code:
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); }
I get this error from 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
It seems that both of them are related to the same problem, which somehow causes list.iter() , trying to give me a tuple (_, String, bool) instead of just (String, bool) . Why is this happening?
source share