当前位置:网站首页>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

边栏推荐
- 开户注册挖财安全吗?有没有风险的?靠谱吗?
- 【PaddleClas】常用命令
- Nanjing University: Discussion on the training program of digital talents in the new era
- Clickhouse (03) how to install and deploy Clickhouse
- MATLAB中print函数使用
- Login and connect CDB and PDB
- Let more young people from Hong Kong and Macao know about Nansha's characteristic cultural and creative products! "Nansha kylin" officially appeared
- pytorch yolov5 训练自定义数据
- [PM2 details]
- Leetcode exercise - 206 Reverse linked list
猜你喜欢

Simulate the hundred prisoner problem

Fix vulnerability - mysql, ES

Use JMeter to record scripts and debug

吴恩达团队2022机器学习课程,来啦

Sophon CE Community Edition is online, and free get is a lightweight, easy-to-use, efficient and intelligent data analysis tool

Star Ring Technology launched transwarp Navier, a data element circulation platform, to help enterprises achieve secure data circulation and collaboration under privacy protection

Wu Enda team 2022 machine learning course, coming
![Whether to take a duplicate subset with duplicate elements [how to take a subset? How to remove duplicates?]](/img/b2/d019c3f0b85a6c0d334a092fa6c23c.png)
Whether to take a duplicate subset with duplicate elements [how to take a subset? How to remove duplicates?]

Cmake tutorial step1 (basic starting point)

Introduction to the development function of Hanlin Youshang system of Hansheng Youpin app
随机推荐
吴恩达团队2022机器学习课程,来啦
Can communication of nano
通过SOCKS代理渗透整个内网
[performance test] full link voltage test
Introduction to VC programming on "suggestions collection"
Tupu software digital twin | visual management system based on BIM Technology
Generate classes from XML schema
mybash
Introduction to Resampling
JVM third talk -- JVM performance tuning practice and high-frequency interview question record
寻找第k小元素 前k小元素 select_k
Xiaobai getting started with NAS - quick building private cloud tutorial series (I) [easy to understand]
VC编程入门浅谈「建议收藏」
彻底理解为什么网络 I/O 会被阻塞?
开户注册股票炒股安全吗?有没有风险的?靠谱吗?
热通孔的有效放置如何改善PCB设计中的热管理?
记一次使用Windbg分析内存“泄漏”的案例
破解湖+仓混合架构顽疾,星环科技推出自主可控云原生湖仓一体平台
Clickhouse (03) how to install and deploy Clickhouse
The 10th global Cloud Computing Conference | Huayun data won the "special contribution award for the 10th anniversary of 2013-2022"