Reputation: 182
Can I use a mutable reference method like a value-passing one? For example, can I use
o.mth(&mut self, ...)
as
o.mth(self, ...)
This would allow me to return the result without worrying about the lifetime of o
. It might involve a move
closure, or some kind of wrapper?
For context, I'm trying to return a boxed iterator over CSV records using the rust-csv package but the iterator can't outlive the reader, which Reader::records(&'t mut self)
borrows mutably. Contrast this with BufRead::lines(self)
, which consumes its reader and hence can be returned without lifetime problems.
Upvotes: 1
Views: 117
Reputation: 431489
No, you cannot. The reason that self
, &self
, and &mut self
methods exist is because they behave differently, have different restrictions, and allow different things.
In this case, you'd probably ultimately end up trying to create an iterator that yields references to itself, which isn't allowed, or store a value and a reference to that value in the same struct, which is also disallowed.
Upvotes: 2