gpt4 book ai didi

pattern-matching - 这个解包/模式匹配代码可以更清晰/惯用吗?

转载 作者:行者123 更新时间:2023-11-29 07:55:37 25 4
gpt4 key购买 nike

作为一个学习项目,我正在探索用 Rust 实现链表的不同方法。在一个特定的地方,我有一些代码可以正常工作,但它会多次调用 unwrap——我的印象是这通常被认为是不安全/糟糕的风格。我想让它变得更好。

以下是一些相关的定义,省略了一些不重要的细节。请注意,它是一个单向链表,拥有 next 指针。这些定义都应该是直截了当的,可以略读的;为了便于阅读,我将把有趣的部分分开。

type NodePtr<T> = Option<Box<Node<T>>>;
struct Node<T> {
data: T,
next: NodePtr<T>,
}
pub struct LinkedList<T> {
head: NodePtr<T>,
}
impl<T> LinkedList<T> {
pub fn pop_back(&mut self) -> Result<T, LinkedListError> {
if self.head.is_none() {
Err(LinkedListError { kind: LinkedListErrorKind::Empty })
} else {
Ok(LinkedList::pop_last_node(&mut self.head))
}
}
// definition for pop_last_node coming up shortly...
}

在这个特定的实现中,我正在试验递归函数,这是我的 pop_last_node 的工作版本。

fn pop_last_node(node_ref: &mut NodePtr<T>) -> T {
match node_ref.as_ref().unwrap().next {
None => {
let old_tail = node_ref.take();
old_tail.unwrap().data
}
_ => LinkedList::pop_last_node(&mut node_ref.as_mut().unwrap().next)
}
}

这工作正常,但由于我是作为一个学习实验来做的,我想看看我是否可以减少解包调用并使用更多的模式匹配。这部分实验进行得并不顺利。

这是我的尝试。不幸的是,这个版本比原来的版本更冗长(而且令人困惑!)。我特别不喜欢“在你可以做任何事情之前就超出这个范围”的部分,但我还没有想出如何让它变得更好的想法。

fn pop_last_node(node_ref: &mut NodePtr<T>) -> T {
{
let next_node = match node_ref.as_mut() {
None => panic!("Error handling will go here..."),
Some(node_box) => &mut node_box.next,
};
match *next_node {
None => {
// fall through to code below
},
_ => {
return LinkedList::pop_last_node(next_node)
},
}
}
// no sense converting this to a match--the "None" case was already checked above
node_ref.take().unwrap().data
}

这就是我现在的位置。主要问题是:是否有一种不那么疯狂的方式来编写模式匹配版本?是否有显着的方法来提高任一版本的清晰度或惯用性?

最佳答案

由于 Box,在稳定版上与框的模式匹配很困惑。如果您愿意每晚使用直到盒子模式稳定,您可以重写您的 pop_back 函数(而不仅仅是 pop_last_node 函数):

pub fn pop_back(&mut self) -> Result<T, LinkedListError> {
fn pop_last_node<T>(node: &mut NodePtr<T>) -> Option<T> {
match node.take() {
None => None,
// is a leaf
Some(box Node { next: None, data }) => Some(data),
Some(mut this) => {
// recurse
let ret = pop_last_node(&mut this.next);
// put this subnode back, since it's not a leaf
*node = Some(this);
ret
}
}
}
pop_last_node(&mut self.head).ok_or(LinkedListError {
kind: LinkedListErrorKind::Empty
})
}

PlayPen 中试用

关于pattern-matching - 这个解包/模式匹配代码可以更清晰/惯用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30971800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com