Why is this a mutable borrow?

I get the error, cannot borrow country_codes as mutable, as it is not declared as mutable, down in the assert! line:

#[tokio::test]
async fn integration_test_schema_country_codes() -> Result<()> {
    let json = get_json("/v0/all").await?;
    let country_codes = json.keys();

    // cannot borrow `country_codes` as mutable, as it is not declared as mutable:
    assert!(country_codes.all(|cc| is_valid_country_code(cc))); 
    Ok(())
}

Is there something about assert! or borrowing that I don't understand? I'm definitely not mutating country_codes because I'm only invoking .all() on it. (?)

What's the cleanest way to write this?

You are mutating country_codes! It's an iterator, and all takes &mut self because it needs to drive the iterator forward to do its job.

You aren't mutating the actual country codes themselves, but you are mutating the position within the list of codes represented by country_codes.

3 Likes

Ah hah! Makes perfect sense. Thanks!

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.