这个问题的名字很糟糕,但基本上,考虑这个简单的例子:
您有一个以下形式的链接列表:
struct Node<T> {
_next: Option<~Node<T>>,
_data: Option<T>
}
以及将元素添加到链中的“推送”操作:
/** Attach a node as the 'next' node in this chain */
fn push<'a>(&'a mut self, value: T) -> &'a mut ~Node<T> {
if (self._next.is_none()) {
self._push_node(~Node::new(value));
} else {
let tmp = self._next.take().unwrap();
let mut next = ~Node::new(value);
next._push_node(tmp);
self._push_node(next);
}
return self._next.as_mut().unwrap();
}
这是可行的,但感觉作为 match() 表达式而不是 if 语句会更简洁,如下所示:
/** Attach a node as the 'next' node in this chain */
fn push<'a>(&'a mut self, value: T) -> &'a mut ~Node<T> {
match self._next {
None => { self._push_node(~Node::new(value)); },
Some(ref v) => { <----------------------------- :(
let tmp = self._next.take().unwrap(); <------- ???
let mut next = ~Node::new(value);
next._push_node(tmp);
self._push_node(next);
}
}
return self._next.as_mut().unwrap();
}
然而,由于上面指出的行,这不会起作用;实际上,我们正在修改 _next 的值,这是不可能发生的,因为我们借用了 self._next 作为 match 语句的 Some() 的范围。
有更好的方法吗?
您能否以某种方式将 match 语句纯粹声明为匹配,以便一旦您进入 Some() => { ... } 块,您就可以not借用价值?
因为Some(ref v)
, you were借用价值。然后你没有使用它,所以Some(_)
本来就很好。但说实话,你do想要取值。所以你真正想要的是改变take()
比赛之外。
这是最终结果:
pub struct Node<T> {
next: Option<~Node<T>>,
data: Option<T>
}
/** Attach a node as the 'next' node in this chain */
pub fn push<'a>(&'a mut self, value: T) -> &'a mut ~Node<T> {
match self.next.take() {
None => self.push_node(~Node::new(value)),
Some(v) => {
let mut next = ~Node::new(value);
next.push_node(v);
self.push_node(next);
}
}
match self.next {
Some(ref mut t) => t,
None => unreachable!(),
}
// Sure, you could replace those four lines with self.next.as_mut().unwrap(),
// but I have a slight leaning towards highlighting the unreachable nature of
// the None branch. It makes it more explicit.
// Others will doubtless disagree with me on that point.
}
你也可以去Some(ref mut v)
并操纵了&mut ~Node<T>
直接价值,但这可能需要改变如何push_node
作品。 (您还没有显示它的代码,所以我只能猜测它的作用。)
我还做了一些其他风格上的改变:
- 四个空格,而不是两个;
- 不要用下划线作为前缀(Rust 有足够的隐私控制,不需要使用下划线);
- 周围没有括号
if
表达式(好吧,现在已被替换为match
,所以没关系);
- 如果匹配分支中只有一个语句,则不需要用大括号括起来 - 只需在其后使用逗号即可,
-
return x;
可以写成x
当它位于函数末尾时。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)