我有一个函数返回了一个Result<&'a ~Foo, BarErr>,可以通过以下方式进行访问:
match x.borrow() {
Ok(ref foo) => println!("Found {}", foo.value),
Err(Nope) => println!("Bleh")
}
然而,我现在发现自己面临一个更加复杂的情况,我希望借用一个可变引用,这样我就可以在它上面调用一个函数:
match x.borrow() {
Ok(ref foo) => { foo.inc(); trace!("Found {}", foo.value); },
Err(Nope) => trace!("Bleh")
}
我尝试了几种方法来确定在哪里需要添加“mut”,例如mut ref foo,ref mut foo,-> mut Result<...>,-> Result,但我似乎无法弄清所需的语法。
我一直得到:
error: cannot borrow immutable dereference of `~`-pointer `***foo` as mutable
它应该是什么?
完整示例代码:
macro_rules! trace(
($($arg:tt)*) => (
{ let x = ::std::io::stdout().write_line(format_args!(::std::fmt::format, $($arg)*)); println!("{}", x); }
);
)
#[deriving(Show)]
struct Foo {
value: int
}
impl Foo {
fn inc(&mut self) {
self.value += 1;
}
}
#[deriving(Show)]
struct Bar {
data: Option<~Foo>
}
#[deriving(Show)]
enum BarErr {
Nope
}
impl Bar {
fn borrow<'a>(&'a mut self) -> Result<&'a ~Foo, BarErr> {
match self.data {
Some(ref e) => return Ok(e),
None => return Err(Nope)
}
}
}
#[test]
fn test_create_indirect() {
let y = ~Foo { value: 10 };
let mut x = Bar { data: Some(y) };
let mut x2 = Bar { data: None };
{
match x.borrow() {
Ok(ref mut foo) => { foo.inc(); trace!("Found {}", foo.value); },
Err(Nope) => trace!("Bleh")
}
}
{
let z = x2.borrow();
trace!("Z: {}", z);
}
}