3个版本
0.1.2 | 2020年3月5日 |
---|---|
0.1.1 | 2020年3月5日 |
0.1.0 | 2020年3月5日 |
#54 in #words
7KB
50 行
kwindex
版权所有 (c) 2020 Ronnie Song
这是一个基于Rust的“关键词索引”库crate,用于维护从文本中提取的词的索引。
pub struct KWIndex<'a>(Vec<&'a str>);
用法
- 使用crate访问函数
extern crate kwindex; use kwindex::*;
- 创建一个新的空目标单词索引
let mut index = KWIndex::new();
- 解析文本并将每个有效单词添加到索引中
index = index.extend_from_text("Hello world.");
- 检查索引是否为空
assert_eq!(true, index.is_empty());
- 统计单词数量
assert_eq!(2, index.len());
- 统计关键词的出现次数
assert_eq!(1, index.count_matches("world"));
运行示例
要运行示例程序,请输入以下命令
cargo run --example example
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target\debug\examples\example.exe`
is_empty(): true
[Hey] is add to KWIndex index
[!] is removed from [world!]
[world] is add to KWIndex index
is_empty(): false
len(): 2
count_matches('world'): 1
KWIndex { word: ["Hey", "world"] }
一切顺利!它解析了文本并将其添加到列表中,没有问题,并成功打印了is_empty检查、长度和匹配次数。
测试
要测试库crate,请输入以下命令
cargo test
Compiling kwindex v0.1.0 (C:\Users\ronsong\Desktop\Docs\rust\kwindex)
Finished test [unoptimized + debuginfo] target(s) in 0.96s
Running target\debug\deps\test-8f8dfddd5ec24cfa.exe
running 6 tests
test kwindex_tests::test_is_empty ... ok
test kwindex_tests::test_count_matches ... ok
test kwindex_tests::test_count_matches2 ... ok
test kwindex_tests::test_is_not_empty ... ok
test kwindex_tests::test_len ... ok
test kwindex_tests::test_len2 ... ok
test result: ok. 6 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
所有测试均通过,没有问题。
测试位于tests/rand.rs文件中,该文件使用std assert_eq!()和assert_ne!()来测试文件中新()、extend_to_text()、count_matches()、len()和is_empty()函数的实际结果与预期结果。
许可证
本程序根据“MIT许可证”授权。请参阅此软件源分布中的LICENSE
文件以获取许可证条款。