当前位置:网站首页>rust统计文件中单词出现的次数
rust统计文件中单词出现的次数
2022-07-05 18:12:00 【pilaf1990】
说明
该例子来源于《精通Rust 第2版》 [印]拉胡尔·沙玛 [芬]韦萨·凯拉维塔 著 邓世超 译 36页。但是可能是rust语言在变化的原因,在笔者rust版本是rustc 1.61.0 (fe5b13d68 2022-05-18)的环境中编译都报错,经过修改部分代码可以运行了。下面是修改后的代码:
代码
use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::prelude::BufRead;
use std::io::BufReader;
// 定义一个Tuple Struct(元组结构体,元组结构体的特点是字段只有类型,没有名称)
#[derive(Debug)]
struct WordCounter(HashMap<String,u64>);
impl WordCounter{
fn new() -> WordCounter{
// 创建元组结构体WordCounter的实例,元组的第一个元素是HashMap类型实例
WordCounter(HashMap::new())
}
// &mut self作为第一个参数,则此方法提供对类型实例的可变访问
fn increment(&mut self, word:&str){
let key = word.to_string();
// self.0表示获取元组结构体WordCounter的第一个元素,即HashMap的实例
let count = self.0.entry(key).or_insert(0);
// 单词次数加1
*count += 1;
}
fn display(self){
for (key,value) in self.0.iter() {
println!("单词:{}出现了{}次", key, value);
}
}
}
fn main() {
let arguments: Vec<String> = env::args().collect();
// 这儿要用&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");
// 每一行文本按照空格分割
let words = line.split(" ");
for word in words {
if word == ""{
continue
} else {
word_counter.increment(word);
}
}
}
word_counter.display();
}
运行效果

边栏推荐
- 开户复杂吗?网上开户安全么?
- Elk log analysis system
- 星环科技重磅推出数据要素流通平台Transwarp Navier,助力企业实现隐私保护下的数据安全流通与协作
- 图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
- 小林coding的内存管理章节
- 在一台服务器上部署多个EasyCVR出现报错“Press any to exit”,如何解决?
- Nacos distributed transactions Seata * * install JDK on Linux, mysql5.7 start Nacos configure ideal call interface coordination (nanny level detail tutorial)
- Sophon CE Community Edition is online, and free get is a lightweight, easy-to-use, efficient and intelligent data analysis tool
- Sophon kg upgrade 3.1: break down barriers between data and liberate enterprise productivity
- 通过SOCKS代理渗透整个内网
猜你喜欢

Fix vulnerability - mysql, ES

小林coding的内存管理章节

Introduction to the development function of Hanlin Youshang system of Hansheng Youpin app

Sophon KG升级3.1:打破数据间壁垒,解放企业生产力

《力扣刷题计划》复制带随机指针的链表

Wu Enda team 2022 machine learning course, coming

图片数据不够?我做了一个免费的图像增强软件

瀚升优品app翰林优商系统开发功能介绍

Sophon kg upgrade 3.1: break down barriers between data and liberate enterprise productivity

第十届全球云计算大会 | 华云数据荣获“2013-2022十周年特别贡献奖”
随机推荐
How to solve the error "press any to exit" when deploying multiple easycvr on one server?
Check namespaces and classes
小白入门NAS—快速搭建私有云教程系列(一)[通俗易懂]
[BeanShell] there are many ways to write data locally
Sophon KG升级3.1:打破数据间壁垒,解放企业生产力
buuctf-pwn write-ups (9)
English sentence pattern reference
记录Pytorch中的eval()和no_grad()
VC编程入门浅谈「建议收藏」
钉钉开放平台小程序API的缓存接口都有哪些内容?
Xiaobai getting started with NAS - quick building private cloud tutorial series (I) [easy to understand]
RSE2020/云检测:基于弱监督深度学习的高分辨率遥感图像精确云检测
[paddleclas] common commands
从类生成XML架构
GIMP 2.10教程「建议收藏」
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
文章中的逻辑词
Matlab built-in function how different colors, matlab subsection function different colors drawing
vulnhub之darkhole_2
从XML架构生成类