当前位置:网站首页>Trust counts the number of occurrences of words in the file
Trust counts the number of occurrences of words in the file
2022-07-05 18:19:00 【pilaf1990】
explain
The example comes from 《 Master Rust The first 2 edition 》 [ print ] Lahore · sharma [ Fen ] VESA · Kelavita Writing Deng Shichao translate 36 page . But it could be rust The reason why language is changing , In the author rust The version is rustc 1.61.0 (fe5b13d68 2022-05-18)
There are errors in the compilation of the environment , After modification, part of the code can run . Here is the modified code :
Code
use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::prelude::BufRead;
use std::io::BufReader;
// Define a Tuple Struct( Tuple structure , The characteristic of tuple structure is that fields only have types , There is no name )
#[derive(Debug)]
struct WordCounter(HashMap<String,u64>);
impl WordCounter{
fn new() -> WordCounter{
// Create tuple structure WordCounter Example , The first element of a tuple is HashMap Type instance
WordCounter(HashMap::new())
}
// &mut self As the first parameter , Then this method provides variable access to type instances
fn increment(&mut self, word:&str){
let key = word.to_string();
// self.0 Represents getting tuple structure WordCounter The first element of , namely HashMap Example
let count = self.0.entry(key).or_insert(0);
// The number of words plus 1
*count += 1;
}
fn display(self){
for (key,value) in self.0.iter() {
println!(" word :{} There is {} Time ", key, value);
}
}
}
fn main() {
let arguments: Vec<String> = env::args().collect();
// I need it here &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");
// Each line of text is separated by spaces
let words = line.split(" ");
for word in words {
if word == ""{
continue
} else {
word_counter.increment(word);
}
}
}
word_counter.display();
}
Running effect
边栏推荐
- [use electron to develop desktop on youqilin]
- Find the first k small element select_ k
- [paddlepaddle] paddedetection face recognition custom data set
- 最大人工岛[如何让一个连通分量的所有节点都记录总节点数?+给连通分量编号]
- Sophon autocv: help AI industrial production and realize visual intelligent perception
- Le cours d'apprentissage de la machine 2022 de l'équipe Wunda arrive.
- 小白入门NAS—快速搭建私有云教程系列(一)[通俗易懂]
- 分享:中兴 远航 30 pro root 解锁BL magisk ZTE 7532N 8040N 9041N 刷机 刷面具原厂刷机包 root方法下载
- About Estimation with Cross-Validation
- 第十一届中国云计算标准和应用大会 | 华云数据成为全国信标委云计算标准工作组云迁移专题组副组长单位副组长单位
猜你喜欢
ViewPager + RecyclerView的内存泄漏
@Extension、@SPI注解原理
ConvMAE(2022-05)
ISPRS2022/雲檢測:Cloud detection with boundary nets基於邊界網的雲檢測
Record a case of using WinDbg to analyze memory "leakage"
Sophon AutoCV:助力AI工业化生产,实现视觉智能感知
使用Jmeter虚拟化table失败
Thoroughly understand why network i/o is blocked?
使用JMeter录制脚本并调试
About statistical power
随机推荐
Eliminate the writing of 'if () else{}'
Introduction to Resampling
第十一届中国云计算标准和应用大会 | 华云数据成为全国信标委云计算标准工作组云迁移专题组副组长单位副组长单位
Wu Enda team 2022 machine learning course, coming
Thoroughly understand why network i/o is blocked?
彻底理解为什么网络 I/O 会被阻塞?
Easynmon Usage Summary
To solve the stubborn problem of Lake + warehouse hybrid architecture, xinghuan Technology launched an independent and controllable cloud native Lake warehouse integrated platform
【PaddleClas】常用命令
How to obtain the coordinates of the aircraft passing through both ends of the radar
MATLAB中print函数使用
nano的CAN通信
JVM第三话 -- JVM性能调优实战和高频面试题记录
英语句式参考
[paddlepaddle] paddedetection face recognition custom data set
Cmake tutorial step1 (basic starting point)
JDBC reads a large amount of data, resulting in memory overflow
[BeanShell] there are many ways to write data locally
[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
GIMP 2.10教程「建议收藏」