当前位置:网站首页>Trust counts the number of occurrences of words in the file
Trust counts the number of occurrences of words in the file
2022-07-23 17:47: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

边栏推荐
- sns_ sensor_ instance_ api
- Single cell literature learning (part6) -- forestfireclustering for SC sequencing combinations iterative label promotion with
- The larger the convolution kernel, the stronger the performance? An interpretation of replknet model
- 基于scrapy的电商平台数据爬取与展示
- 面试官:如何用 Redis 实现分布式锁?
- Geometric parametric reconstruction
- LeetCode_动态规划_中等_120.三角形最小路径和
- [operation] Yan Yi (Internet new technology operation)
- 数据集中度分析,数据分布情况
- 5秒到1秒,记一次效果“非常”显著的性能优化
猜你喜欢

分析optimism重放合约地址攻击事件

File management system based on OpenPGP

Console calculator developed based on C language

Pymoo learning (4): multi criteria decision making

Kv260 single board PS control setting IIC switch chip

可视化机房管理

MySQL大量写入问题优化方案 MySQL参数调优

Software configuration | pychart download, installation, environment configuration and uninstall

Kubernetes kubelet manages pod core process
![[operation] Yan Yi (Internet new technology operation)](/img/7a/38c7a9ed79e626506de067f360384c.png)
[operation] Yan Yi (Internet new technology operation)
随机推荐
Emgu cv3+c # image processing (IV): use emgucv to obtain camera and read video
Food safety | drinking fresh milk may infect tuberculosis? Take you to know what is milk sterilization
Software configuration | Anaconda download, installation, environment configuration and uninstall
Single cell thesis record (part19) -- a comprehensive comparison on cell type composition information for St data
Food safety | ham sausage lunch meat, is it really so unbearable?
元素内容必须由格式正确的字符数据或标记组成
Do you really understand the persistence mechanism of redis?
MySQL大量写入问题优化方案 MySQL参数调优
Kubernetes focuses on kubelet's responsibilities
Données chronologiques dans l'Internet industriel des objets
rust求数组中最大值
Thread pool, who am I? Where am I?
从马尔可夫链到GPT,字节跳动AI Lab总监李航细说语言模型的前世今生
网页基础模版
深拷贝deepClone的实现
[JS] check whether the date object is invalid date
Literature learning (part100) -- an introduction to autoencoders
xlinx pcie xvc
TYPE-C 转OTG(USB2.0传输数据)+PD充电协议芯片 乐得瑞LDR6028/LDR6023SS
Encapsulate the general connection and query of the project with pymysql