Hey Rustaceans! Got an easy question? Ask here (28/2020)!

I have a function with the signature fn push(&mut self, item: Item) -> Result<(), Item>

Basically, the function checks item and self, and if everything's right, it pushes item into a data structure in self (so ownership is transferred).

Otherwise, the checks fail. In that case ownership of the original item is returned via the Err of the Result. This is how some std functions handle this situation of conditionally transferring ownership.

The self.push(item) is done near the end of the function, after all checks have passed. The problem is that because I try to early-return item, it is considered moved and then I can't later push it into self. Is there any way around this? Do I really have to restrict the return Err(item) part to a single place at the end of the function?

/r/rust Thread