- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
作为一个学习项目,我正在探索用 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/
考虑以下两个应用程序: 1: public partial class MainWindow : Window { public MainWindow() { Init
我写了一些被https://github.com/protobuf-c/protobuf-c/wiki/Examples引用的c代码 Operation msg = OPERATION__INIT;
我在使用 SwiftyJSON 正确解开字符串时遇到问题 output: ["AAPL"] stockData = try Data(contentsOf: url!)
我有一个只包含属性的类,我想对其进行打包/解包。我应该实现什么 collections.abc 才能获得此行为? class Item(object): def __init__(self,
考虑以下表达式。请注意,某些表达式被重复以呈现“上下文”。 (这是一个很长的 list ) a, b = 1, 2 # simple sequence
我正在尝试将变量表作为参数解包到函数调用中。简而言之,我正在考虑通过这样做将依赖项注入(inject)到我的过程中。当我在 https://www.lua.org/cgi-bin/demo 执行此操作
我正在尝试弄清楚如何赋予对象解包值的能力。 我想出的用例如下: 让我们有一个 Interval 类,我们将使用它来评估实值函数。 我们想问 成员资格,因此 __contains__。 通过调用特定步骤
所以我知道有一些库可以为我做到这一点,但我想学习打包/解包。 我的目标是让用户输入 IP 地址/子网掩码,然后验证其是否有效。 我想到的一种方法是“sprintf”并获得一个二进制值,比如说 192.
所以我知道有一些库可以为我做到这一点,但我想学习打包/解包。 我的目标是让用户输入 IP 地址/子网掩码,然后验证其是否有效。 我想到的一种方法是“sprintf”并获得一个二进制值,比如说 192.
我正在通过Go v1.13 Go v1.14中的错误跟踪进行检查。为什么看起来只有errors.Is()才能找到没有参数或带有值接收器的错误实现?这意味着能够包装的错误实现必须具有一个值接收器,以便能
我需要解包由其他开发人员创建的 Oracle 包。我在我的数据库中创建了 Prackage,但采用加密格式。 我需要的原因是,原来的开发人员已经离开了组织,现在包中定义的过程需要重新定义,以更新 DB
我打算在文件中使用 unpack。首先我用一个字符串测试。当我在字符串中嵌入空格时,下面的脚本将其显示为空。当我测试文件空间时,它正在被正确读取。不确定为什么在我处理字符串时将其更改为 null。我可
我在 Perl 中遇到这个问题已经有几天了,在搜索了无数的手册页、perldocs 和谷歌搜索了太多的搜索词之后,希望这里有人能帮助我。 我得到两个表示十六进制值的字符串,即“FFFF”,而不是 Pe
在 Python 中解压 SequenceMatcher 循环结果以便轻松访问和处理值的最佳方法是什么? from difflib import * orig = "1234567890" comme
假设,我有一个列表, [(1,2), (3, 4)]. 如果列表中的所有元素都是元组,我将打印 1 + 2 和 3 + 4。但是,如果任何一个元素也是一个列表,那么我将 1 加到内部列表的每个元素,并
我有这样的字典列表。 rows = [ {'user': staff_user, 'grade': [u'0.0', u'N/A', u'N/A', u'N/A', u'N/A']}, {'user'
我有这个代码... function a(options) { for (var item in options) { if ( ! options.hasOwnProperty
我试图通过 std::tie 解压一个 std::array: #include #include int main() { std::array arr = {1, 2, 3};
我的数据包嗅探器有问题。目标端口和源端口在我的嗅探器中似乎是错误的。在 wireshark 中,端口与我的嗅探器完全不同。没有结果包含预期来自 TLS 的端口 443。 (整个 tcp 片段可能是错误
如何使用 php 以大端字节顺序打包/解包 float ?我通过解包功能走到了这一步,但我不确定这是否可行。 function unpackFloat ($float) { $n = unpa
我是一名优秀的程序员,十分优秀!