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
91
92
93
94
95
96
97
98
|
#[derive(Debug, PartialEq)]
struct Document {
title: String,
body: String,
}
impl Document {
fn new(title: String, body: String) -> Self {
Self {
title,
body,
}
}
fn title(&self) -> &str {
self.title.as_ref()
}
fn body(&self) -> &str {
self.body.as_ref()
}
}
impl AsRef<str> for Document {
fn as_ref(&self) -> &str {
self.body()
}
}
fn word_count<T: AsRef<str>>(value: T) -> usize {
value.as_ref().split_whitespace().count()
}
fn contains_text<T, U>(haystack: T, needle: U) -> bool
where
T: AsRef<str>,
U: AsRef<str>,
{
haystack.as_ref().contains(needle.as_ref())
}
fn main() {
()
}
#[cfg(test)]
mod tests {
use crate::*;
#[test]
fn test_document() {
let document = Document::new("title".to_string(), "body".to_string());
assert_eq!(document.title(), "title");
assert_eq!(document.body(), "body");
assert_eq!(document.as_ref(), document.body());
}
#[test]
fn test_word_count() {
let document = Document::new("title".to_string(), "one two three".to_string());
assert_eq!(word_count(&document), 3);
assert_eq!(word_count(document), 3);
assert_eq!(word_count("one two three".to_string()), 3);
assert_eq!(word_count("one two three"), 3);
assert_eq!(word_count(""), 0);
assert_eq!(word_count(" "), 0);
assert_eq!(word_count("one\ttwo\nthree"), 3);
}
#[test]
fn test_contains_text() {
assert!(contains_text("a b c", "a"));
assert!(contains_text("a b c", ""));
assert!(!contains_text("a b c", "z"));
assert!(!contains_text("a b c", "A"));
assert!(contains_text("a b c".to_string(), "a".to_string()));
assert!(!contains_text("a b c".to_string(), "z".to_string()));
let d0 = Document::new("t".to_string(), "a b c".to_string());
let d1 = Document::new("t".to_string(), "a".to_string());
assert!(contains_text(&d0, d1));
assert!(!contains_text(&d0, "z".to_string()));
}
#[test]
fn test_document_reuse() {
let doc = Document::new(
"Notes".to_string(),
"Rust ownership matters".to_string(),
);
assert_eq!(word_count(&doc), 3);
assert!(contains_text(&doc, "ownership"));
assert!(contains_text(&doc, "Rust"));
assert_eq!(doc.title(), "Notes");
assert_eq!(doc.body(), "Rust ownership matters");
}
}
|