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

边栏推荐
- 最大人工岛[如何让一个连通分量的所有节点都记录总节点数?+给连通分量编号]
- New words new words new words new words [2]
- 南京大学:新时代数字化人才培养方案探讨
- Simulate the hundred prisoner problem
- English sentence pattern reference
- Matlab built-in function how different colors, matlab subsection function different colors drawing
- Sophon KG升级3.1:打破数据间壁垒,解放企业生产力
- Fix vulnerability - mysql, ES
- 爱因斯坦求和einsum
- Tupu software digital twin | visual management system based on BIM Technology
猜你喜欢

吳恩達團隊2022機器學習課程,來啦

模拟百囚徒问题

About Estimation with Cross-Validation

让更多港澳青年了解南沙特色文创产品!“南沙麒麟”正式亮相

Simulate the hundred prisoner problem

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

Le cours d'apprentissage de la machine 2022 de l'équipe Wunda arrive.
![[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](/img/a6/aa0b8d30913dc64f3c0cd891528c40.png)
[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

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

Thoroughly understand why network i/o is blocked?
随机推荐
Wu Enda team 2022 machine learning course, coming
兄弟组件进行传值(显示有先后顺序)
修复漏洞 - mysql 、es
Sophon base 3.1 launched mlops function to provide wings for the operation of enterprise AI capabilities
Simulate the hundred prisoner problem
Huaxia Fund: sharing of practical achievements of digital transformation in the fund industry
Sophon kg upgrade 3.1: break down barriers between data and liberate enterprise productivity
Fix vulnerability - mysql, ES
LeetCode 6111. 螺旋矩阵 IV
VC编程入门浅谈「建议收藏」
Sophon CE Community Edition is online, and free get is a lightweight, easy-to-use, efficient and intelligent data analysis tool
图像分类,看我就够啦!
《2022中国信创生态市场研究及选型评估报告》发布 华云数据入选信创IT基础设施主流厂商!
About statistical power
Matlab built-in function how different colors, matlab subsection function different colors drawing
【在优麒麟上使用Electron开发桌面应】
[QNX Hypervisor 2.2用户手册]6.3.2 配置VM
Introduction to Resampling
开户复杂吗?网上开户安全么?
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?