practice_runLengthEncoding/rust_impl/src/main.rs

38 lines
954 B
Rust
Raw Normal View History

2021-02-19 05:38:52 +00:00
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
#[structopt(name = "RunLengthEncoding")]
struct Opts {
input: String,
}
2021-02-19 02:08:41 +00:00
fn main() {
2021-02-19 05:38:52 +00:00
let opts = Opts::from_args();
2021-08-18 11:31:47 +00:00
let mut current: Option<char> = None;
let mut current_count: u32 = 0;
let mut list: Vec<(char, u32)> = Vec::new();
2021-02-19 05:38:52 +00:00
for c in opts.input.chars() {
2021-08-18 11:31:47 +00:00
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));
2021-02-19 05:38:52 +00:00
}
2021-08-18 11:31:47 +00:00
for (c, count) in list {
println!("{}: has count of {}", c, count);
2021-02-19 05:38:52 +00:00
}
2021-02-19 02:08:41 +00:00
}