Trouble passing reference to matched value to a function

Hi All

I just started programming Rust this week. I've been reading through the book and creating a small CLI tool to try things out. So far I love it :slight_smile:

I've got a struct which is generated from a JSON file. I would like to update a value (end) in this struct but the value is optional. Here's what I've got so far.

    fn update_end(state: &mut TrackingItem) {
        let end = Utc::now().format("%Y-%m-%d %H:%M:%S").to_string();
        state.end = Some(end);
    }

    fn stop_current_tracking(config: &mut TurboTrackConfig) -> bool{
        match &config.current {
            None => false,
            Some(item) => {
                update_end(&mut item);
                true
            }
        }
    }

I get the following error:

cannot borrow `item` as mutable, as it is not declared as mutable

But now I am stuck. If someone could tell me how to fix this as well as a resource where I can learn more about this problem? I've been reading the book but I am only at chapter 7 ATM :slight_smile:

Thanks in advance!

The immediate cause of the error is that in the binding Some(item), item is not mutable. You could make it mutable (Some(mut item)), but that wouldn't help, because you are borrowing config.current immutably in the first place, and you can't mutate through an immutable reference.

To resolve the root cause of your problem, write:

    fn stop_current_tracking(config: &mut TurboTrackConfig) -> bool {
        match config.current {
            None => false,
            Some(ref mut item) => {
                update_end(item);
                true
            }
        }
    }

Thank you so much. I'll read up on ref :slight_smile: