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

Function par_checker2

code/chapter03/par_checker2.rs:38–69  ·  view source on GitHub ↗
(par: &str)

Source from the content-addressed store, hash-verified

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