구조체 함수가 특별한 조건 하에서 스스로를 호출하기를 원합니다. 필드의 하나로서 HashMap
이있을 때 작동했지만, 을 Vec
으로 변경했을 때 깨졌습니다. 그것은 심지어 사용 되어질 필요가 없으며, 매우 이상하게 보이고 이에 대한 합당한 설명을 찾을 수 없습니다.은`* self`을 변경할 수 없기 때문에 빌릴 수 없습니다.
use std::vec::Vec;
use std::collections::HashMap;
struct Foo<'a> {
bar: Vec<&'a str>
//bar: HashMap<&'a str, &'a str>
}
impl<'a> Foo<'a> {
pub fn new() -> Foo<'a> {
Foo { bar: Vec::new() }
//Foo { bar: HashMap::new() }
}
pub fn baz(&'a self) -> Option<int> {
None
}
pub fn qux(&'a mut self, retry: bool) {
let opt = self.baz();
if retry { self.qux(false); }
}
}
pub fn main() {
let mut foo = Foo::new();
foo.qux(true);
}
놀이 틀 : http://is.gd/GgMy79
오류 :
나는이 문제를 해결할 수있는 방법<anon>:22:24: 22:28 error: cannot borrow `*self` as mutable because it is also borrowed as immutable
<anon>:22 if retry { self.qux(false); }
^~~~
<anon>:21:23: 21:27 note: previous borrow of `*self` occurs here; the immutable borrow prevents subsequent moves or mutable borrows of `*self` until the borrow ends
<anon>:21 let opt = self.baz();
^~~~
<anon>:23:10: 23:10 note: previous borrow ends here
<anon>:20 pub fn qux(&'a mut self, retry: bool) {
<anon>:21 let opt = self.baz();
<anon>:22 if retry { self.qux(false); }
<anon>:23 }
? 이 원인이 #6268일까요?
방법'바즈의 정의()'에서'A'를 제거 제출했다. 왜 그것이 그것을 일으키는 지 모르겠다. – Levans