I thought I understand it

Hi, I'm creating file explorer, something like mc, and I have an fn:

fn fill_table_with_items(a_table: &mut tableViewType, a_dir: &PathBuf) 

Then, later in code I try to call it which gives me error saying that a_table needs to be mutable:

 table.set_on_submit(move |siv: &mut Cursive, row: usize, index: usize| {
        let value = siv
            .call_on_name(a_name, move |a_table: &mut tableViewType| {//HERE I'm getting the error
/*But if I change it to (note the extra mut in front of  a_tabe) then it works:
mut a_table: &mut tableViewType
*/
                let path: String = a_table.borrow_item(index).unwrap().name.clone();
                fill_table_with_items(&mut a_table, &std::path::PathBuf::from(path));
            })
            .unwrap();

So my question is:
Why do I have to write:

mut a_table: &mut tableViewType

My fn fill_table_with_items(a_table: &mut tableViewType, a_dir: &PathBuf)
takes a_table by &mut so I thought that passing it by &mut would be the correct way to do it which is not. Could somebody please explain that to me?

Thanks

Does it work without the extra mut if you do this?

let value = siv
    .call_on_name(a_name, move |a_table: &mut tableViewType| {
        let path: String = a_table.borrow_item(index).unwrap().name.clone();
        // Remove the &mut here:
        fill_table_with_items(a_table, &std::path::PathBuf::from(path));
})

a_table is already a mutable reference, so adding &mut in front is creating a &mut &mut tableViewType (i.e. a mutable reference to the a_table binding, which is itself a mutable reference to data that lives elsewhere).

1 Like

Ah yes! Thanks!

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.