-
Notifications
You must be signed in to change notification settings - Fork 520
/
main.rs
90 lines (76 loc) · 2.26 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#![feature(test)]
extern crate test;
use test::Bencher;
fn main() {
println!("Hello, world!");
}
pub fn reply_match(msg: &str) -> &str {
let message = msg.trim_end();
if message.is_empty() {
return "Fine. Be that way!";
}
let is_questioning = message.ends_with('?');
let is_yelling =
message.chars().any(|ch| ch.is_alphabetic()) && message == message.to_uppercase();
match (is_yelling, is_questioning) {
(true, true) => "Calm down, I know what I'm doing!",
(true, _) => "Whoa, chill out!",
(_, true) => "Sure.",
_ => "Whatever.",
}
}
pub fn reply_if_chain(msg: &str) -> &str {
let message = msg.trim_end();
if message.is_empty() {
return "Fine. Be that way!";
}
let is_questioning = message.ends_with('?');
let is_yelling =
message.chars().any(|ch| ch.is_alphabetic()) && message == message.to_uppercase();
if is_yelling {
return if is_questioning {
"Calm down, I know what I'm doing!"
} else {
"Whoa, chill out!"
};
}
if is_questioning {
return "Sure.";
}
"Whatever."
}
const ANSWERS: &'static [&'static str] = &[
"Whatever.",
"Sure.",
"Whoa, chill out!",
"Calm down, I know what I'm doing!",
];
pub fn reply_array(msg: &str) -> &str {
let message = msg.trim_end();
if message.is_empty() {
return "Fine. Be that way!";
}
let is_questioning = if message.ends_with('?') { 1 } else { 0 };
let is_yelling =
if message.chars().any(|ch| ch.is_alphabetic()) && message == message.to_uppercase() {
2
} else {
0
};
ANSWERS[is_questioning + is_yelling]
}
#[bench]
/// multiple line question for match
fn multiple_line_question_match(b: &mut Bencher) {
b.iter(|| reply_match("\rDoes this cryogenic chamber make me look fat?\rNo."));
}
#[bench]
/// multiple line question for if statements
fn multiple_line_question_if(b: &mut Bencher) {
b.iter(|| reply_if_chain("\rDoes this cryogenic chamber make me look fat?\rNo."));
}
#[bench]
/// multiple line question for answer array
fn multiple_line_question_array(b: &mut Bencher) {
b.iter(|| reply_array("\rDoes this cryogenic chamber make me look fat?\rNo."));
}