【发布时间】:2014-11-14 20:18:00
【问题描述】:
我希望我的 struct 函数在特殊条件下调用自己。当我将HashMap 作为字段之一时它起作用了,但是当我将HashMap 更改为Vec 时它就坏了。它甚至不必使用,这看起来很奇怪,我找不到任何合理的解释。
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造成的?
【问题讨论】:
-
删除方法
baz()定义中的'a。不知道为什么会导致它。
标签: rust