当前位置:网站首页>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();
}
运行效果

边栏推荐
- Can communication of nano
- Whether to take a duplicate subset with duplicate elements [how to take a subset? How to remove duplicates?]
- About Statistical Power(统计功效)
- 集群部署如何解决海量视频接入与大并发需求?
- 登录连接 CDB 和 PDB
- 彻底理解为什么网络 I/O 会被阻塞?
- 第十一届中国云计算标准和应用大会 | 华云数据成为全国信标委云计算标准工作组云迁移专题组副组长单位副组长单位
- 图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
- 深拷贝与浅拷贝【面试题3】
- 吴恩达团队2022机器学习课程,来啦
猜你喜欢

Introduction to the development function of Hanlin Youshang system of Hansheng Youpin app

Star ring technology data security management platform defender heavy release

Fix vulnerability - mysql, ES

瀚升优品app翰林优商系统开发功能介绍

To solve the stubborn problem of Lake + warehouse hybrid architecture, xinghuan Technology launched an independent and controllable cloud native Lake warehouse integrated platform

pytorch yolov5 训练自定义数据

ConvMAE(2022-05)

模拟百囚徒问题

Daily exercise: a series of dates

buuctf-pwn write-ups (9)
随机推荐
第十一届中国云计算标准和应用大会 | 云计算国家标准及白皮书系列发布 华云数据全面参与编制
Leetcode notes: Weekly contest 300
小白入门NAS—快速搭建私有云教程系列(一)[通俗易懂]
写作写作写作写作
Le cours d'apprentissage de la machine 2022 de l'équipe Wunda arrive.
OpenShift常用管理命令杂记
Leetcode daily practice: rotating arrays
[paddlepaddle] paddedetection face recognition custom data set
What is the reason why the video cannot be played normally after the easycvr access device turns on the audio?
钉钉开放平台小程序API的缓存接口都有哪些内容?
Simulate the hundred prisoner problem
Isprs2022 / Cloud Detection: Cloud Detection with Boundary nets Boundary Networks Based Cloud Detection
Sophon autocv: help AI industrial production and realize visual intelligent perception
Multithreading (I) processes and threads
JDBC reads a large amount of data, resulting in memory overflow
English sentence pattern reference
Crontab 日志:如何记录我的 Cron 脚本的输出
Thoroughly understand why network i/o is blocked?
[performance test] full link voltage test
IDC report: Tencent cloud database ranks top 2 in the relational database market!