当前位置:网站首页>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
边栏推荐
- 吴恩达团队2022机器学习课程,来啦
- 从类生成XML架构
- Matlab built-in function how different colors, matlab subsection function different colors drawing
- pytorch yolov5 训练自定义数据
- Vulnhub's darkhole_ two
- Penetrate the whole intranet through socks agent
- Tupu software digital twin | visual management system based on BIM Technology
- Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]
- Check namespaces and classes
- nacos -分布式事务-Seata** linux安装jdk ,mysql5.7启动nacos配置ideal 调用接口配合 (保姆级细节教程)
猜你喜欢
How to obtain the coordinates of the aircraft passing through both ends of the radar
Sophon kg upgrade 3.1: break down barriers between data and liberate enterprise productivity
Memory management chapter of Kobayashi coding
Image classification, just look at me!
The 2022 China Xinchuang Ecological Market Research and model selection evaluation report released that Huayun data was selected as the mainstream manufacturer of Xinchuang IT infrastructure!
Privacy computing helps secure data circulation and sharing
Sophon AutoCV:助力AI工业化生产,实现视觉智能感知
mybash
使用Jmeter虚拟化table失败
To solve the stubborn problem of Lake + warehouse hybrid architecture, xinghuan Technology launched an independent and controllable cloud native Lake warehouse integrated platform
随机推荐
nacos -分布式事务-Seata** linux安装jdk ,mysql5.7启动nacos配置ideal 调用接口配合 (保姆级细节教程)
Sibling components carry out value transfer (there is a sequence displayed)
Eliminate the writing of 'if () else{}'
[utiliser Electron pour développer le Bureau sur youkirin devrait]
Leetcode exercise - 206 Reverse linked list
Multithreading (I) processes and threads
在一台服务器上部署多个EasyCVR出现报错“Press any to exit”,如何解决?
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
如何获取飞机穿过雷达两端的坐标
Sophon CE Community Edition is online, and free get is a lightweight, easy-to-use, efficient and intelligent data analysis tool
Nacos distributed transactions Seata * * install JDK on Linux, mysql5.7 start Nacos configure ideal call interface coordination (nanny level detail tutorial)
node_exporter内存使用率不显示
爱因斯坦求和einsum
How to obtain the coordinates of the aircraft passing through both ends of the radar
[TestLink] testlink1.9.18 solutions to common problems
JVM第三话 -- JVM性能调优实战和高频面试题记录
数值计算方法 Chapter8. 常微分方程的数值解
Easynmon Usage Summary
个人对卷积神经网络的理解
Tencent music launched its new product "quyimai", which provides music commercial copyright authorization