当前位置:网站首页>rust统计文件中单词出现的次数
rust统计文件中单词出现的次数
2022-07-05 18:12:00 【pilaf1990】
说明
该例子来源于《精通Rust 第2版》 [印]拉胡尔·沙玛 [芬]韦萨·凯拉维塔 著 邓世超 译 36页。但是可能是rust语言在变化的原因,在笔者rust版本是rustc 1.61.0 (fe5b13d68 2022-05-18)的环境中编译都报错,经过修改部分代码可以运行了。下面是修改后的代码:
代码
use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::prelude::BufRead;
use std::io::BufReader;
// 定义一个Tuple Struct(元组结构体,元组结构体的特点是字段只有类型,没有名称)
#[derive(Debug)]
struct WordCounter(HashMap<String,u64>);
impl WordCounter{
fn new() -> WordCounter{
// 创建元组结构体WordCounter的实例,元组的第一个元素是HashMap类型实例
WordCounter(HashMap::new())
}
// &mut self作为第一个参数,则此方法提供对类型实例的可变访问
fn increment(&mut self, word:&str){
let key = word.to_string();
// self.0表示获取元组结构体WordCounter的第一个元素,即HashMap的实例
let count = self.0.entry(key).or_insert(0);
// 单词次数加1
*count += 1;
}
fn display(self){
for (key,value) in self.0.iter() {
println!("单词:{}出现了{}次", key, value);
}
}
}
fn main() {
let arguments: Vec<String> = env::args().collect();
// 这儿要用&arguments
let filename = &arguments[1];
println!("Processing file:{}",filename);
let file = File::open(filename).expect("Could not open file");
let reader = BufReader::new(file);
let mut word_counter = WordCounter::new();
for line in reader.lines() {
let line = line.expect("Could not read line");
// 每一行文本按照空格分割
let words = line.split(" ");
for word in words {
if word == ""{
continue
} else {
word_counter.increment(word);
}
}
}
word_counter.display();
}
运行效果

边栏推荐
- jdbc读大量数据导致内存溢出
- GFS distributed file system
- Easynmon Usage Summary
- [performance test] full link voltage test
- FCN: Fully Convolutional Networks for Semantic Segmentation
- Cmake tutorial step1 (basic starting point)
- [BeanShell] there are many ways to write data locally
- Electron安装问题
- Leetcode daily practice: rotating arrays
- 吴恩达团队2022机器学习课程,来啦
猜你喜欢
![Whether to take a duplicate subset with duplicate elements [how to take a subset? How to remove duplicates?]](/img/b2/d019c3f0b85a6c0d334a092fa6c23c.png)
Whether to take a duplicate subset with duplicate elements [how to take a subset? How to remove duplicates?]

Image classification, just look at me!

U-Net: Convolutional Networks for Biomedical Images Segmentation

Daily exercise: a series of dates
![含重复元素取不重复子集[如何取子集?如何去重?]](/img/b2/d019c3f0b85a6c0d334a092fa6c23c.png)
含重复元素取不重复子集[如何取子集?如何去重?]
![Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]](/img/8b/a60fc36115580f018445e4c2a28a9d.png)
Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]

隐私计算助力数据的安全流通与共享

第十一届中国云计算标准和应用大会 | 云计算国家标准及白皮书系列发布 华云数据全面参与编制

IDC report: Tencent cloud database ranks top 2 in the relational database market!

星环科技数据安全管理平台 Defensor重磅发布
随机推荐
模拟百囚徒问题
[TestLink] testlink1.9.18 solutions to common problems
多线程(一) 进程与线程
Daily exercise: a series of dates
ConvMAE(2022-05)
使用Jmeter虚拟化table失败
兄弟组件进行传值(显示有先后顺序)
访问数据库使用redis作为mysql的缓存(redis和mysql结合)
JDBC reads a large amount of data, resulting in memory overflow
ISPRS2022/雲檢測:Cloud detection with boundary nets基於邊界網的雲檢測
[utiliser Electron pour développer le Bureau sur youkirin devrait]
How to improve the thermal management in PCB design with the effective placement of thermal through holes?
【pm2详解】
吴恩达团队2022机器学习课程,来啦
从XML架构生成类
【在优麒麟上使用Electron开发桌面应】
[paddleclas] common commands
[use electron to develop desktop on youqilin]
破解湖+仓混合架构顽疾,星环科技推出自主可控云原生湖仓一体平台
第十一届中国云计算标准和应用大会 | 华云数据成为全国信标委云计算标准工作组云迁移专题组副组长单位副组长单位