MCPcopy Create free account
hub / github.com/QMHTMY/RustBook / par_checker2

Function par_checker2

publication/code/chapter04/par_checker2.rs:40–71  ·  view source on GitHub ↗
(par: &str)

Source from the content-addressed store, hash-verified

38}
39
40fn par_checker2(par: &str) -> bool {
41 let mut char_list = Vec::new();
42 for c in par.chars() {
43 char_list.push(c);
44 }
45
46 let mut index = 0;
47 let mut balance = true;
48 let mut stack = Stack::new();
49 while index < char_list.len() && balance {
50 let c = char_list[index];
51
52 // 同时判断三种开符号
53 if '(' == c || '[' == c || '{' == c {
54 stack.push(c);
55 } else {
56 if stack.is_empty() {
57 balance = false;
58 } else {
59 // 比较当前括号和栈顶括号是否匹配
60 let top = stack.pop().unwrap();
61 if !par_match(top, c) {
62 balance = false;
63 }
64 }
65 }
66
67 index += 1;
68 }
69
70 balance && stack.is_empty()
71}
72
73fn main() {
74 let sa = "(){}[]";

Callers 1

mainFunction · 0.70

Calls 5

par_matchFunction · 0.70
pushMethod · 0.45
lenMethod · 0.45
is_emptyMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected