Here is a code fragment:
let field_name = parse_string(json_cursor)?;
skip_whitespace(json_cursor);
json_cursor.expect_char(u32::from(':'))?;
if target_field.unwrap_or_default() == field_name {
In the first line I call parse_string which does a mutable borrow of json_cursor. The result that it returns does not contain a reference to json_cursor
. However the compiler is assuming that field_name
does reference json_cursor
. Field_name is used in the last line of the code fragment. For this reason, the compiler believes that the mutable borrow of json_cursor
that started in the first line does not end until the last line.
Because of this incorrect inference, the compiler is complaining that the second line is an error, because it believes that the call to skip_whitespace
is trying to do a mutable borrow of json_cursor
before the first borrow has ended.
Does anyone know how to tell the compiler that the result of parse_string
does NOT contain a reference to its argument? Here is the way that parse_string
is declared:
pub fn parse_string<'a>(json_cursor: &'a mut JsonCursor) -> Result<&'a [u32], anyhow::Error> {
I suspect that I need to say something different about the lifetime of the result, but not specifying a lifetime does not work.