Skip to content

Commit d5b6071

Browse files
author
Richard Dodd
committed
Add a simple example of library use.
Specifically for internment without any static values.
1 parent 89e0ef8 commit d5b6071

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

examples/simple.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
extern crate string_cache;
2+
3+
use string_cache::{DefaultAtom, Atom};
4+
5+
fn main() {
6+
7+
let mut interned_stuff = Vec::new();
8+
let text = "here is a sentence of text that will be tokenised and interned and some repeated \
9+
tokens is of text and";
10+
for word in text.split_whitespace() {
11+
let seen_before = interned_stuff.iter()
12+
// We can use impl PartialEq<T> where T is anything string-like to compare to
13+
// interned strings to either other interned strings, or actual strings Comparing two
14+
// interned strings is very fast (normally a single cpu operation).
15+
.filter(|interned_word| interned_word == &word)
16+
.count();
17+
if seen_before > 0 {
18+
println!(r#"Seen the word "{}" {} times"#, word, seen_before);
19+
} else {
20+
println!(r#"Not seen the word "{}" before"#, word);
21+
}
22+
// We use the impl From<(Cow<'a, str>, or &'a str, or String) for Atom<Static> to intern a
23+
// new string
24+
interned_stuff.push(DefaultAtom::from(word));
25+
}
26+
}

0 commit comments

Comments
 (0)