当前位置:网站首页>rust统计文件中单词出现的次数
rust统计文件中单词出现的次数
2022-07-23 15:05: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();
}
运行效果

边栏推荐
- 【redis入门系列】redis的数据类型及相关命令
- Pymoo learning (2): Bi objective optimization problems with constraints
- Transfer business append log (transaction propagation behavior)
- Food safety | eight things you must know when choosing probiotic products
- 单细胞文献学习(part6)--ForestFireClustering for sc sequencing combines iterative label propagation with ...
- 转账业务追加日志(事务的传播行为).
- 数智化时代文旅遇新机?中国移动咪咕造 “元宇宙第一岛”
- Kubernetes kubelet manages pod core process
- Food safety chocolate is also true or false? How much do you know about it
- computed在项目中的使用
猜你喜欢

KV260单板PS控制设置IIC开关芯片

Kubernetes kubelet manages pod core process

基于scrapy的电商平台数据爬取与展示

ride the wind and waves! Digital transformation in the era of financial technology

59. General knowledge of lightning safety

el-input使用

Interviewer: how to use redis to realize distributed locks?

Kubernetes Kubelet管理pod核心流程

使用moment获取当天日期与下一天

USB通信协议深入理解
随机推荐
SAP HANA数据库备份失败解决办法
Date formatting
Leetcode skimming: dynamic planning 04 (different paths)
[introduction series of redis] data types and related commands of redis
MySQL大量写入问题优化方案 MySQL参数调优
How to refine the operation of small program mall?
Unity production QR code scanning
xlinx pcie xvc
At least half of the people can't answer the difference between isempty and isblank
Analyze optimism replay contract address attack events
Don't ask me again why MySQL hasn't left the index? For these reasons, I'll tell you all
网页返回更新
The larger the convolution kernel, the stronger the performance? An interpretation of replknet model
What about the new retail e-commerce platform? Can we realize the digital transformation of traditional retail enterprises?
Single cell thesis record (part19) -- a comprehensive comparison on cell type composition information for St data
JS tool CECP
基于策略路由部署的网络多出口设计研究与实现
File management system based on OpenPGP
Transfer business append log (transaction propagation behavior)
MySQL7种JOIN(图)