当前位置:网站首页>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();
}
运行效果
边栏推荐
- 开户复杂吗?网上开户安全么?
- 南京大学:新时代数字化人才培养方案探讨
- [BeanShell] there are many ways to write data locally
- Whether to take a duplicate subset with duplicate elements [how to take a subset? How to remove duplicates?]
- 寻找第k小元素 前k小元素 select_k
- Nacos distributed transactions Seata * * install JDK on Linux, mysql5.7 start Nacos configure ideal call interface coordination (nanny level detail tutorial)
- 图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
- MATLAB中print函数使用
- Star ring technology data security management platform defender heavy release
- ISPRS2022/云检测:Cloud detection with boundary nets基于边界网的云检测
猜你喜欢
瀚升优品app翰林优商系统开发功能介绍
Elk log analysis system
nano的CAN通信
FCN: Fully Convolutional Networks for Semantic Segmentation
[JMeter] advanced writing method of JMeter script: all variables, parameters (parameters can be configured by Jenkins), functions, etc. in the interface automation script realize the complete business
Record eval() and no in pytoch_ grad()
华夏基金:基金行业数字化转型实践成果分享
pytorch yolov5 训练自定义数据
Sophon AutoCV:助力AI工业化生产,实现视觉智能感知
Unicode processing in response of flash interface
随机推荐
IDC report: Tencent cloud database ranks top 2 in the relational database market!
Penetrate the whole intranet through socks agent
To solve the stubborn problem of Lake + warehouse hybrid architecture, xinghuan Technology launched an independent and controllable cloud native Lake warehouse integrated platform
【PaddleClas】常用命令
Sophon CE社区版上线,免费Get轻量易用、高效智能的数据分析工具
Record eval() and no in pytoch_ grad()
JDBC reads a large amount of data, resulting in memory overflow
Crontab 日志:如何记录我的 Cron 脚本的输出
钉钉开放平台小程序API的缓存接口都有哪些内容?
怎么选择外盘期货平台最正规安全?
Sophon autocv: help AI industrial production and realize visual intelligent perception
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
使用JMeter录制脚本并调试
模拟百囚徒问题
Leetcode exercise - 206 Reverse linked list
Numerical calculation method chapter8 Numerical solutions of ordinary differential equations
Eliminate the writing of 'if () else{}'
从XML架构生成类
Sophon Base 3.1 推出MLOps功能,为企业AI能力运营插上翅膀
Record a case of using WinDbg to analyze memory "leakage"