Skip to content

Instantly share code, notes, and snippets.

@BrianKopp
Last active June 16, 2019 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrianKopp/43da8bfc760cf1909008f1a3fd5936f0 to your computer and use it in GitHub Desktop.
Save BrianKopp/43da8bfc760cf1909008f1a3fd5936f0 to your computer and use it in GitHub Desktop.
Rust CLI to read file and print word count
use std::env;
use std::fs;
use std::process;
use std::io;
fn main() {
let args: Vec<String> = env::args().collect();
let file_name = &args[1];
let file_content_result = fs::read_to_string(file_name);
if let Err(err) = file_content_result {
match err.kind() {
io::ErrorKind::NotFound => println!("file '{}' not found", file_name),
_ => println!("unexpected error occurred: {}", err),
}
process::exit(1)
}
let contents = file_content_result.unwrap();
println!("{}", content);
}
use std::env;
use std::fs;
use std::process;
use std::io;
use std::collections::HashMap;
extern crate regex;
use regex::Regex;
fn main() {
let args: Vec<String> = env::args().collect();
let file_name = &args[1];
let file_content_result = fs::read_to_string(file_name);
if let Err(err) = file_content_result {
match err.kind() {
io::ErrorKind::NotFound => println!("file '{}' not found", file_name),
_ => println!("unexpected error occurred: {}", err),
}
process::exit(1)
}
let contents = file_content_result.unwrap();
// create regular expression
let re = Regex::new(r"[a-zA-Z_$][a-zA-Z_$0-9]*").unwrap();
let mut counts_by_word: HashMap<&str, u32> = HashMap::new();
for content_match in re.find_iter(&contents) {
let count = counts_by_word
.entry(content_match.as_str())
.or_insert(0);
*count += 1;
}
println!("Results:");
let mut sorted_words: Vec<_> = counts_by_word.iter().collect();
sorted_words.sort_by(|a, b| a.1.partial_cmp(b.1).unwrap());
for result in sorted_words.iter() {
println!("{} - {}", result.0, result.1);
}
}
On the other hand, we denounce with righteous indignation
and dislike men who are so beguiled and demoralized by the
charms of pleasure of the moment, so blinded by desire,
that they cannot foresee the pain and trouble that are bound
to ensue; and equal blame belongs to those who fail in their
duty through weakness of will, which is the same as saying
through shrinking from toil and pain. These cases are
perfectly simple and easy to distinguish. In a free hour,
when our power of choice is untrammelled and when nothing
prevents our being able to do what we like best, every
pleasure is to be welcomed and every pain avoided. But in
certain circumstances and owing to the claims of duty or
the obligations of business it will frequently occur that
pleasures have to be repudiated and annoyances accepted.
The wise man therefore always holds in these matters to
this principle of selection: he rejects pleasures to secure
other greater pleasures, or else he endures pains to avoid worse pains.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment