Fix rust_impl

This commit is contained in:
Stephen Seo 2021-08-18 20:31:47 +09:00
parent fcb1d2ed04
commit 0e5f84c3bf
3 changed files with 35 additions and 18 deletions

22
rust_impl/Cargo.lock generated
View file

@ -1,11 +1,6 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]] version = 3
name = "RunLengthEncoding"
version = "0.1.0"
dependencies = [
"structopt",
]
[[package]] [[package]]
name = "ansi_term" name = "ansi_term"
@ -120,6 +115,13 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "run_length_encoding"
version = "0.1.0"
dependencies = [
"structopt",
]
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.8.0" version = "0.8.0"
@ -128,9 +130,9 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]] [[package]]
name = "structopt" name = "structopt"
version = "0.3.21" version = "0.3.22"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5277acd7ee46e63e5168a80734c9f6ee81b1367a7d8772a2d765df2a3705d28c" checksum = "69b041cdcb67226aca307e6e7be44c8806423d83e018bd662360a93dabce4d71"
dependencies = [ dependencies = [
"clap", "clap",
"lazy_static", "lazy_static",
@ -139,9 +141,9 @@ dependencies = [
[[package]] [[package]]
name = "structopt-derive" name = "structopt-derive"
version = "0.4.14" version = "0.4.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ba9cdfda491b814720b6b06e0cac513d922fc407582032e8706e9f137976f90" checksum = "7813934aecf5f51a54775e00068c237de98489463968231a51746bbbc03f9c10"
dependencies = [ dependencies = [
"heck", "heck",
"proc-macro-error", "proc-macro-error",

View file

@ -1,5 +1,5 @@
[package] [package]
name = "RunLengthEncoding" name = "run_length_encoding"
version = "0.1.0" version = "0.1.0"
authors = ["Stephen Seo <seo.disparate@gmail.com>"] authors = ["Stephen Seo <seo.disparate@gmail.com>"]
edition = "2018" edition = "2018"
@ -7,4 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
structopt = "0.3.21" structopt = "0.3.22"

View file

@ -1,5 +1,4 @@
use structopt::StructOpt; use structopt::StructOpt;
use std::collections::HashMap;
#[derive(StructOpt, Debug)] #[derive(StructOpt, Debug)]
#[structopt(name = "RunLengthEncoding")] #[structopt(name = "RunLengthEncoding")]
@ -10,13 +9,29 @@ struct Opts {
fn main() { fn main() {
let opts = Opts::from_args(); let opts = Opts::from_args();
let mut map: HashMap<char, u32> = HashMap::new(); let mut current: Option<char> = None;
let mut current_count: u32 = 0;
let mut list: Vec<(char, u32)> = Vec::new();
for c in opts.input.chars() { for c in opts.input.chars() {
map.get_mut(&c).map(|v| *v += 1).or_else(|| {map.insert(c, 1); Some(())}); if c.is_ascii_whitespace() {
continue;
} else {
if current.is_none() || current.unwrap() != c {
if current_count > 0 {
list.push((current.unwrap(), current_count));
}
current_count = 1;
current = Some(c);
} else {
current_count += 1;
}
}
}
if current_count > 0 && current.is_some() {
list.push((current.unwrap(), current_count));
} }
for (c, value) in map { for (c, count) in list {
println!("{} with value {}", c, value); println!("{}: has count of {}", c, count);
} }
} }