Understanding an unexpected async-fn borrow checker error

The async part is not relevant; you will get the same error with async and await deleted. This is the borrow checker not fully understanding uses of borrows in conditional control flow. It sees that the usage of &self.data potentially extends “to the end of the function body” (if returned), and therefore conflicts with the self.update() borrow occurring later in the function body; it doesn’t see that those two cases are mutually exclusive because the if cannot both return and continue.

The workaround is to avoid taking the borrow that you will return until you know for certain that you are going to return it; this is what using ref data does, because it delays the borrowing of self.data until after the match succeeds.

1 Like