Skip to main content

error_message

Function error_message 

Source
pub fn error_message<'a>(title: &'a str, content: &'a str) -> MessageChoice
Expand description

Display a simple error message box. The message box has for style MessageButtons::Ok and MessageIcons::Error. It is recommended to use modal_error_message because it locks the window that creates the message box. This method may be deprecated in the future

Parameters:

  • title: The message box title
  • content: The message box message
Examples found in repository?
examples/calculator.rs (line 72)
54    fn compute(&self) {
55        use Token::*;
56        static SYMBOLS: &'static [char] = &['+', '-', '*', '/'];
57
58        let eq = self.input.text();
59        if eq.len() == 0 {
60            return;
61        }
62
63        let mut tokens: Vec<Token> = Vec::with_capacity(5);
64        let mut last = 0;
65
66        for (i, chr) in eq.char_indices() {
67            if SYMBOLS.iter().any(|&s| s == chr) {
68                let left = &eq[last..i];
69                match left.parse::<i32>() {
70                    Ok(i) => tokens.push(Token::Number(i)),
71                    _ => {
72                        nwg::error_message("Error", "Invalid equation!");
73                        self.input.set_text("");
74                        return;
75                    }
76                }
77
78                let tk = match chr {
79                    '+' => Plus,
80                    '-' => Minus,
81                    '*' => Mult,
82                    '/' => Div,
83                    _ => unreachable!(),
84                };
85
86                tokens.push(tk);
87
88                last = i + 1;
89            }
90        }
91
92        let right = &eq[last..];
93        match right.parse::<i32>() {
94            Ok(i) => tokens.push(Token::Number(i)),
95            _ => {
96                nwg::error_message("Error", "Invalid equation!");
97                self.input.set_text("");
98                return;
99            }
100        }
101
102        let mut i = 1;
103        let mut result = match &tokens[0] {
104            Token::Number(n) => *n,
105            _ => unreachable!(),
106        };
107        while i < tokens.len() {
108            match [&tokens[i], &tokens[i + 1]] {
109                [Plus, Number(n)] => {
110                    result += n;
111                }
112                [Minus, Number(n)] => {
113                    result -= n;
114                }
115                [Mult, Number(n)] => {
116                    result *= n;
117                }
118                [Div, Number(n)] => {
119                    result /= n;
120                }
121                _ => unreachable!(),
122            }
123            i += 2;
124        }
125
126        self.input.set_text(&result.to_string());
127    }