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

边栏推荐
- 最大人工岛[如何让一个连通分量的所有节点都记录总节点数?+给连通分量编号]
- Introduction to the development function of Hanlin Youshang system of Hansheng Youpin app
- 寻找第k小元素 前k小元素 select_k
- 苹果手机炒股安全吗?打新债是骗局吗?
- 兄弟组件进行传值(显示有先后顺序)
- Penetrate the whole intranet through socks agent
- 从类生成XML架构
- Multithreading (I) processes and threads
- node_ Exporter memory usage is not displayed
- Login and connect CDB and PDB
猜你喜欢

Sophon KG升级3.1:打破数据间壁垒,解放企业生产力

Sophon Base 3.1 推出MLOps功能,为企业AI能力运营插上翅膀

U-Net: Convolutional Networks for Biomedical Images Segmentation

Sophon AutoCV:助力AI工业化生产,实现视觉智能感知

星环科技重磅推出数据要素流通平台Transwarp Navier,助力企业实现隐私保护下的数据安全流通与协作

华夏基金:基金行业数字化转型实践成果分享

第十届全球云计算大会 | 华云数据荣获“2013-2022十周年特别贡献奖”

使用QT遍历Json文档及搜索子对象

Copy the linked list with random pointer in the "Li Kou brush question plan"

buuctf-pwn write-ups (9)
随机推荐
Unicode processing in response of flash interface
【PaddlePaddle】 PaddleDetection 人脸识别 自定义数据集
Clickhouse (03) how to install and deploy Clickhouse
English sentence pattern reference
记一次使用Windbg分析内存“泄漏”的案例
mybash
New words new words new words new words [2]
How to solve the error "press any to exit" when deploying multiple easycvr on one server?
兄弟组件进行传值(显示有先后顺序)
星环科技数据安全管理平台 Defensor重磅发布
How can cluster deployment solve the needs of massive video access and large concurrency?
Leetcode notes: Weekly contest 300
node_exporter内存使用率不显示
彻底理解为什么网络 I/O 会被阻塞?
图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
Notes on common management commands of openshift
Can communication of nano
Tupu software digital twin | visual management system based on BIM Technology
JDBC reads a large amount of data, resulting in memory overflow