访问时如何避免可选的检查Vec?
while !self.stack.is_empty() {
let top_path;
if let Some(top) = self.stack.last() {
top_path = top.to_vec();
} else {
panic!("error (but can't happen, becouse the stack can't be empty becouse of the while loop)");
}
self.stack.pop();
self.apply_grammar(top_path);
}
Run Code Online (Sandbox Code Playgroud)
有两个问题:
if let...声明(但我知道我不需要它)else panic,因为没有它top_path可能是未初始化的( - >错误).这是我的错误还是Rust?
irc帮助我得到以下答案:
while let Some(top) = self.stack.pop() {
let top_path = top.to_vec();
let mut is_terminal = self.tree.root.is_terminal( top.to_vec() );
self.apply_grammar(top_path);
}
Run Code Online (Sandbox Code Playgroud)
这看起来好多了.thx <3
| 归档时间: |
|
| 查看次数: |
162 次 |
| 最近记录: |