File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments