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
use std::io::stdin;
pub fn head(word: &String) -> String {
word.chars().take(1).collect()
}
pub fn substr(word: &String, start:usize, length:usize) -> String {
let res: String = word.chars().skip(start).take(length).collect();
res
}
pub fn concat(str1: &String, str2: &String) -> String {
format!("{}{}", str1, str2)
}
pub fn contains(main: &String, sub: &String) -> bool {
main[..].contains(&sub[..])
}
pub fn read() -> String {
let mut temp = String::new();
stdin().read_line(& mut temp).expect("failed to read user input");
temp
}
#[cfg(test)]
mod tests {
#[test]
fn head_hello_h() {
assert_eq!("h", ::head(&"hello".to_string()));
}
#[test]
fn substr_hello_1_3() {
let val = String::from("Hello");
assert_eq!("ell", ::substr(&val, 1, 3));
}
#[test]
fn concat_hello_goodbye() {
let val1 = String::from("Hello");
let val2 = String::from("Goodbye");
assert_eq!("HelloGoodbye", ::concat(&val1, &val2));
}
#[test]
fn contains_hello_hell() {
let val1 = String::from("hello");
let val2 = String::from("hell");
assert_eq!(true, ::contains(&val1, &val2));
}
}