我可以使用像值传递的可变引用方法吗?例如,我可以使用
o.mth(&mut self, ...)
如
o.mth(self, ...)
这将允许我返回结果而不用担心 o
的生命周期 . 它可能涉及 move
闭包,或某种包装?
对于上下文,我'm trying to return a boxed iterator over CSV records using the rust-csv package but the iterator can'比读者活得更长,Reader::records(&'t mut self)可以接受 . 与BufRead::lines(self)相比,它消耗了它的读卡器,因此可以在没有生命周期问题的情况下返回 .
1 回答
你不能 . 存在
self
,&self
和&mut self
方法的原因是因为它们的行为不同,具有不同的限制,并允许不同的东西 .在这种情况下,你最终可能最终会尝试to create an iterator that yields references to itself,这是不允许的,或store a value and a reference to that value in the same struct,这也是不允许的 .