I have setup the docker container to run the model evaluation.
Write a self-contained Rust program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,": 1, "an": 1, "example": 1} Don't import any crates that would require a Cargo.toml file. I'll just compile it with `rustc yourfile.rs` and it should work. The program should be run by calling /path/to/rust_word_count with no arguments Do not import serde_json, write json output from scratch.
Write a self-contained Rust program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,": 1, "an": 1, "example": 1} Don't import any crates that would require a Cargo.toml file. I'll just compile it with `rustc yourfile.rs` and it should work. The program should be run by calling /path/to/rust_word_count with no arguments Do not import serde_json, write json output from scratch.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | use std::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader}; fn main() { let file_path = "/tmp/file.txt"; let file = File::open(file_path).expect("Failed to open file"); let reader = BufReader::new(file); let mut word_count: HashMap<String, u32> = HashMap::new(); for line in reader.lines() { let line = line.expect("Failed to read line"); let words = line.split_whitespace(); for word in words { *word_count.entry(word.to_string()).or_insert(0) += 1; } } print_json_output(&word_count); } fn print_json_output(word_count: &HashMap<String, u32>) { print!("{{"); for (i, (word, count)) in word_count.iter().enumerate() { if i > 0 { print!(", "); } print!("\"{}\": {}", word, count); } println!("}}"); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | fn main() { let text = "This is is is, an example"; let mut word_count = std::collections::HashMap::new(); for word in text.split_whitespace() { *word_count.entry(word.to_string()).or_insert(0) += 1; } print_json_output(&word_count); } fn print_json_output(word_count: &std::collections::HashMap<String, u32>) { print!("{{"); let mut first_entry = true; for (word, count) in word_count { if !first_entry { print!(", "); } print!("\"{}\": {}", word, count); first_entry = false; } println!("}}"); } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | use std::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader}; fn main() { let file_path = "/tmp/file.txt"; let file = File::open(file_path).expect("Failed to open file"); let reader = BufReader::new(file); let mut word_count: HashMap<String, u32> = HashMap::new(); for line in reader.lines() { let line = line.expect("Failed to read line"); let words = line.split_whitespace(); for word in words { *word_count.entry(word.to_string()).or_insert(0) += 1; } } print_json_output(&word_count); } fn print_json_output(word_count: &HashMap<String, u32>) { print!("{{"); for (i, (word, count)) in word_count.iter().enumerate() { if i > 0 { print!(", "); } print!("\"{}\": {}", word, count); } println!("}}"); } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | use std::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader}; fn main() { let file_path = "/tmp/file.txt"; let file = File::open(file_path).expect("Failed to open file"); let reader = BufReader::new(file); let mut word_count: HashMap<String, u32> = HashMap::new(); for line in reader.lines() { let line = line.expect("Failed to read line"); let words = line.split_whitespace(); for word in words { *word_count.entry(word.to_string()).or_insert(0) += 1; } } print_json_output(&word_count); } fn print_json_output(word_count: &HashMap<String, u32>) { print!("{{"); for (i, (word, count)) in word_count.iter().enumerate() { if i > 0 { print!(", "); } print!("\"{}\": {}", word, count); } println!("}}"); } |
{"Heaven,": 1, "belief,": 1, "us,": 2, "to": 1, "winter": 1, "other": 1, "hope,": 1, "age": 2, "incredulity,": 1, "times,": 2, "Light,": 1, "spring": 1, "the": 11, "direct": 2, "worst": 1, "wisdom,": 1, "it": 10, "of": 10, "Darkness,": 1, "all": 2, "we": 4, "before": 2, "everything": 1, "best": 1, "season": 2, "way": 1, "foolishness,": 1, "going": 2, "had": 2, "epoch": 2, "was": 10, "nothing": 1, "were": 2, "despair,": 1}
I extracted the following JSON from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | { "Heaven,": 1, "belief,": 1, "us,": 2, "to": 1, "winter": 1, "other": 1, "hope,": 1, "age": 2, "incredulity,": 1, "times,": 2, "Light,": 1, "spring": 1, "the": 11, "direct": 2, "worst": 1, "wisdom,": 1, "it": 10, "of": 10, "Darkness,": 1, "all": 2, "we": 4, "before": 2, "everything": 1, "best": 1, "season": 2, "way": 1, "foolishness,": 1, "going": 2, "had": 2, "epoch": 2, "was": 10, "nothing": 1, "were": 2, "despair,": 1 } |
Testing if the previous output matches the JSON: {
"the": 11,
"it": 10,
"was": 10,
"of": 10,
"we": 4,
"times,": 2,
"age": 2,
"epoch": 2,
"season": 2,
"had": 2,
"before": 2,
"us,": 2,
"were": 2,
"all": 2,
"going": 2,
"direct": 2,
"best": 1,
"worst": 1,
"wisdom,": 1,
"foolishness,": 1,
"belief,": 1,
"incredulity,": 1,
"Light,": 1,
"Darkness,": 1,
"spring": 1,
"hope,": 1,
"winter": 1,
"despair,": 1,
"everything": 1,
"nothing": 1,
"to": 1,
"Heaven,": 1,
"other": 1,
"way": 1
}
: True
I have setup the docker container to run the model evaluation.
Write a self-contained Rust program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,": 1, "an": 1, "example": 1} Don't import any crates that would require a Cargo.toml file. I'll just compile it with `rustc yourfile.rs` and it should work. The program should be run by calling /path/to/rust_word_count with no arguments Do not import serde_json, write json output from scratch.
Write a self-contained Rust program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,": 1, "an": 1, "example": 1} Don't import any crates that would require a Cargo.toml file. I'll just compile it with `rustc yourfile.rs` and it should work. The program should be run by calling /path/to/rust_word_count with no arguments Do not import serde_json, write json output from scratch.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | use std::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader}; fn main() { let file = File::open("/tmp/file.txt").expect("Could not open file"); let reader = BufReader::new(file); let mut word_count: HashMap<String, u32> = HashMap::new(); for line in reader.lines() { if let Ok(line) = line { let words = line.split_whitespace(); for word in words { *word_count.entry(word.to_string()).or_insert(0) += 1; } } } print_json(word_count); } fn print_json(word_count: HashMap<String, u32>) { print!("{{"); for (i, (word, count)) in word_count.iter().enumerate() { print!("\"{}\": {}", word, count); if i < word_count.len() - 1 { print!(", "); } } println!("}}"); } |
1 2 | rustc rust_word_count.rs
./rust_word_count
|
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | use std::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader}; fn main() { let file = File::open("/tmp/file.txt").expect("Could not open file"); let reader = BufReader::new(file); let mut word_count: HashMap<String, u32> = HashMap::new(); for line in reader.lines() { if let Ok(line) = line { let words = line.split_whitespace(); for word in words { *word_count.entry(word.to_string()).or_insert(0) += 1; } } } print_json(word_count); } fn print_json(word_count: HashMap<String, u32>) { print!("{{"); for (i, (word, count)) in word_count.iter().enumerate() { print!("\"{}\": {}", word, count); if i < word_count.len() - 1 { print!(", "); } } println!("}}"); } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | use std::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader}; fn main() { let file = File::open("/tmp/file.txt").expect("Could not open file"); let reader = BufReader::new(file); let mut word_count: HashMap<String, u32> = HashMap::new(); for line in reader.lines() { if let Ok(line) = line { let words = line.split_whitespace(); for word in words { *word_count.entry(word.to_string()).or_insert(0) += 1; } } } print_json(word_count); } fn print_json(word_count: HashMap<String, u32>) { print!("{{"); for (i, (word, count)) in word_count.iter().enumerate() { print!("\"{}\": {}", word, count); if i < word_count.len() - 1 { print!(", "); } } println!("}}"); } |
{"Heaven,": 1, "best": 1, "Light,": 1, "way": 1, "everything": 1, "times,": 2, "were": 2, "it": 10, "was": 10, "belief,": 1, "spring": 1, "winter": 1, "nothing": 1, "us,": 2, "Darkness,": 1, "season": 2, "hope,": 1, "before": 2, "all": 2, "of": 10, "epoch": 2, "to": 1, "worst": 1, "the": 11, "wisdom,": 1, "despair,": 1, "direct": 2, "we": 4, "age": 2, "incredulity,": 1, "other": 1, "had": 2, "going": 2, "foolishness,": 1}
I extracted the following JSON from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | { "Heaven,": 1, "best": 1, "Light,": 1, "way": 1, "everything": 1, "times,": 2, "were": 2, "it": 10, "was": 10, "belief,": 1, "spring": 1, "winter": 1, "nothing": 1, "us,": 2, "Darkness,": 1, "season": 2, "hope,": 1, "before": 2, "all": 2, "of": 10, "epoch": 2, "to": 1, "worst": 1, "the": 11, "wisdom,": 1, "despair,": 1, "direct": 2, "we": 4, "age": 2, "incredulity,": 1, "other": 1, "had": 2, "going": 2, "foolishness,": 1 } |
Testing if the previous output matches the JSON: {
"the": 11,
"it": 10,
"was": 10,
"of": 10,
"we": 4,
"times,": 2,
"age": 2,
"epoch": 2,
"season": 2,
"had": 2,
"before": 2,
"us,": 2,
"were": 2,
"all": 2,
"going": 2,
"direct": 2,
"best": 1,
"worst": 1,
"wisdom,": 1,
"foolishness,": 1,
"belief,": 1,
"incredulity,": 1,
"Light,": 1,
"Darkness,": 1,
"spring": 1,
"hope,": 1,
"winter": 1,
"despair,": 1,
"everything": 1,
"nothing": 1,
"to": 1,
"Heaven,": 1,
"other": 1,
"way": 1
}
: True
I have setup the docker container to run the model evaluation.
Write a self-contained Rust program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,": 1, "an": 1, "example": 1} Don't import any crates that would require a Cargo.toml file. I'll just compile it with `rustc yourfile.rs` and it should work. The program should be run by calling /path/to/rust_word_count with no arguments Do not import serde_json, write json output from scratch.
Write a self-contained Rust program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,": 1, "an": 1, "example": 1} Don't import any crates that would require a Cargo.toml file. I'll just compile it with `rustc yourfile.rs` and it should work. The program should be run by calling /path/to/rust_word_count with no arguments Do not import serde_json, write json output from scratch.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | use std::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader}; fn main() { let file = File::open("/tmp/file.txt").expect("Failed to open file"); let reader = BufReader::new(file); let mut word_count: HashMap<String, u32> = HashMap::new(); for line in reader.lines() { if let Ok(line) = line { let words = line.split_whitespace(); for word in words { *word_count.entry(word.to_string()).or_insert(0) += 1; } } } print_json(word_count); } fn print_json(word_count: HashMap<String, u32>) { print!("{{"); for (i, (word, count)) in word_count.iter().enumerate() { print!("\"{}\": {}", word, count); if i < word_count.len() - 1 { print!(", "); } } println!("}}"); } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | use std::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader}; fn main() { let file = File::open("/tmp/file.txt").expect("Failed to open file"); let reader = BufReader::new(file); let mut word_count: HashMap<String, u32> = HashMap::new(); for line in reader.lines() { if let Ok(line) = line { let words = line.split_whitespace(); for word in words { *word_count.entry(word.to_string()).or_insert(0) += 1; } } } print_json(word_count); } fn print_json(word_count: HashMap<String, u32>) { print!("{{"); for (i, (word, count)) in word_count.iter().enumerate() { print!("\"{}\": {}", word, count); if i < word_count.len() - 1 { print!(", "); } } println!("}}"); } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | use std::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader}; fn main() { let file = File::open("/tmp/file.txt").expect("Failed to open file"); let reader = BufReader::new(file); let mut word_count: HashMap<String, u32> = HashMap::new(); for line in reader.lines() { if let Ok(line) = line { let words = line.split_whitespace(); for word in words { *word_count.entry(word.to_string()).or_insert(0) += 1; } } } print_json(word_count); } fn print_json(word_count: HashMap<String, u32>) { print!("{{"); for (i, (word, count)) in word_count.iter().enumerate() { print!("\"{}\": {}", word, count); if i < word_count.len() - 1 { print!(", "); } } println!("}}"); } |
{"had": 2, "belief,": 1, "of": 10, "everything": 1, "direct": 2, "other": 1, "all": 2, "incredulity,": 1, "spring": 1, "was": 10, "wisdom,": 1, "nothing": 1, "season": 2, "us,": 2, "despair,": 1, "the": 11, "worst": 1, "best": 1, "hope,": 1, "winter": 1, "way": 1, "before": 2, "it": 10, "we": 4, "to": 1, "going": 2, "epoch": 2, "times,": 2, "were": 2, "Light,": 1, "Darkness,": 1, "age": 2, "foolishness,": 1, "Heaven,": 1}
I extracted the following JSON from that output:
1 | {"had": 2, "belief,": 1, "of": 10, "everything": 1, "direct": 2, "other": 1, "all": 2, "incredulity,": 1, "spring": 1, "was": 10, "wisdom,": 1, "nothing": 1, "season": 2, "us,": 2, "despair,": 1, "the": 11, "worst": 1, "best": 1, "hope,": 1, "winter": 1, "way": 1, "before": 2, "it": 10, "we": 4, "to": 1, "going": 2, "epoch": 2, "times,": 2, "were": 2, "Light,": 1, "Darkness,": 1, "age": 2, "foolishness,": 1, "Heaven,": 1} |
Testing if the previous output matches the JSON: {
"the": 11,
"it": 10,
"was": 10,
"of": 10,
"we": 4,
"times,": 2,
"age": 2,
"epoch": 2,
"season": 2,
"had": 2,
"before": 2,
"us,": 2,
"were": 2,
"all": 2,
"going": 2,
"direct": 2,
"best": 1,
"worst": 1,
"wisdom,": 1,
"foolishness,": 1,
"belief,": 1,
"incredulity,": 1,
"Light,": 1,
"Darkness,": 1,
"spring": 1,
"hope,": 1,
"winter": 1,
"despair,": 1,
"everything": 1,
"nothing": 1,
"to": 1,
"Heaven,": 1,
"other": 1,
"way": 1
}
: True
I have setup the docker container to run the model evaluation.
Write a self-contained Rust program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,": 1, "an": 1, "example": 1} Don't import any crates that would require a Cargo.toml file. I'll just compile it with `rustc yourfile.rs` and it should work. The program should be run by calling /path/to/rust_word_count with no arguments Do not import serde_json, write json output from scratch.
Write a self-contained Rust program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,": 1, "an": 1, "example": 1} Don't import any crates that would require a Cargo.toml file. I'll just compile it with `rustc yourfile.rs` and it should work. The program should be run by calling /path/to/rust_word_count with no arguments Do not import serde_json, write json output from scratch.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | use std::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader}; fn main() { let file = File::open("/tmp/file.txt").expect("Unable to open file"); let reader = BufReader::new(file); let mut word_count = HashMap::new(); for line in reader.lines() { let line = line.expect("Unable to read line"); let words = line.split_whitespace(); for word in words { *word_count.entry(word.to_string()).or_insert(0) += 1; } } print_json(&word_count); } fn print_json(word_count: &HashMap<String, i32>) { print!("{{"); let mut first = true; for (word, count) in word_count { if !first { print!(", "); } print!("\"{}\": {}", word, count); first = false; } println!("}}"); } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | use std::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader}; fn main() { let file = File::open("/tmp/file.txt").expect("Unable to open file"); let reader = BufReader::new(file); let mut word_count = HashMap::new(); for line in reader.lines() { let line = line.expect("Unable to read line"); let words = line.split_whitespace(); for word in words { *word_count.entry(word.to_string()).or_insert(0) += 1; } } print_json(&word_count); } fn print_json(word_count: &HashMap<String, i32>) { print!("{{"); let mut first = true; for (word, count) in word_count { if !first { print!(", "); } print!("\"{}\": {}", word, count); first = false; } println!("}}"); } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | use std::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader}; fn main() { let file = File::open("/tmp/file.txt").expect("Unable to open file"); let reader = BufReader::new(file); let mut word_count = HashMap::new(); for line in reader.lines() { let line = line.expect("Unable to read line"); let words = line.split_whitespace(); for word in words { *word_count.entry(word.to_string()).or_insert(0) += 1; } } print_json(&word_count); } fn print_json(word_count: &HashMap<String, i32>) { print!("{{"); let mut first = true; for (word, count) in word_count { if !first { print!(", "); } print!("\"{}\": {}", word, count); first = false; } println!("}}"); } |
{"epoch": 2, "times,": 2, "incredulity,": 1, "had": 2, "we": 4, "best": 1, "season": 2, "was": 10, "hope,": 1, "all": 2, "us,": 2, "foolishness,": 1, "everything": 1, "worst": 1, "of": 10, "belief,": 1, "other": 1, "spring": 1, "age": 2, "to": 1, "way": 1, "were": 2, "winter": 1, "despair,": 1, "nothing": 1, "going": 2, "before": 2, "it": 10, "the": 11, "direct": 2, "Heaven,": 1, "Light,": 1, "Darkness,": 1, "wisdom,": 1}
I extracted the following JSON from that output:
1 | {"epoch": 2, "times,": 2, "incredulity,": 1, "had": 2, "we": 4, "best": 1, "season": 2, "was": 10, "hope,": 1, "all": 2, "us,": 2, "foolishness,": 1, "everything": 1, "worst": 1, "of": 10, "belief,": 1, "other": 1, "spring": 1, "age": 2, "to": 1, "way": 1, "were": 2, "winter": 1, "despair,": 1, "nothing": 1, "going": 2, "before": 2, "it": 10, "the": 11, "direct": 2, "Heaven,": 1, "Light,": 1, "Darkness,": 1, "wisdom,": 1} |
Testing if the previous output matches the JSON: {
"the": 11,
"it": 10,
"was": 10,
"of": 10,
"we": 4,
"times,": 2,
"age": 2,
"epoch": 2,
"season": 2,
"had": 2,
"before": 2,
"us,": 2,
"were": 2,
"all": 2,
"going": 2,
"direct": 2,
"best": 1,
"worst": 1,
"wisdom,": 1,
"foolishness,": 1,
"belief,": 1,
"incredulity,": 1,
"Light,": 1,
"Darkness,": 1,
"spring": 1,
"hope,": 1,
"winter": 1,
"despair,": 1,
"everything": 1,
"nothing": 1,
"to": 1,
"Heaven,": 1,
"other": 1,
"way": 1
}
: True
I have setup the docker container to run the model evaluation.
Write a self-contained Rust program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,": 1, "an": 1, "example": 1} Don't import any crates that would require a Cargo.toml file. I'll just compile it with `rustc yourfile.rs` and it should work. The program should be run by calling /path/to/rust_word_count with no arguments Do not import serde_json, write json output from scratch.
Write a self-contained Rust program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,": 1, "an": 1, "example": 1} Don't import any crates that would require a Cargo.toml file. I'll just compile it with `rustc yourfile.rs` and it should work. The program should be run by calling /path/to/rust_word_count with no arguments Do not import serde_json, write json output from scratch.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | use std::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader}; fn main() { let file_path = "/tmp/file.txt"; let file = match File::open(file_path) { Ok(file) => file, Err(_) => { eprintln!("Error: Unable to open file"); return; } }; let mut word_count: HashMap<String, u32> = HashMap::new(); let reader = BufReader::new(file); for line in reader.lines() { let line = match line { Ok(line) => line, Err(_) => continue, }; let words = line.split_whitespace(); for word in words { *word_count.entry(word.to_string()).or_insert(0) += 1; } } print_json_output(&word_count); } fn print_json_output(word_count: &HashMap<String, u32>) { print!("{{"); let mut first = true; for (word, count) in word_count { if !first { print!(", "); } print!("\"{}\": {}", word, count); first = false; } println!("}}"); } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | use std::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader}; fn main() { let file_path = "/tmp/file.txt"; let file = match File::open(file_path) { Ok(file) => file, Err(_) => { eprintln!("Error: Unable to open file"); return; } }; let mut word_count: HashMap<String, u32> = HashMap::new(); let reader = BufReader::new(file); for line in reader.lines() { let line = match line { Ok(line) => line, Err(_) => continue, }; let words = line.split_whitespace(); for word in words { *word_count.entry(word.to_string()).or_insert(0) += 1; } } print_json_output(&word_count); } fn print_json_output(word_count: &HashMap<String, u32>) { print!("{{"); let mut first = true; for (word, count) in word_count { if !first { print!(", "); } print!("\"{}\": {}", word, count); first = false; } println!("}}"); } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | use std::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader}; fn main() { let file_path = "/tmp/file.txt"; let file = match File::open(file_path) { Ok(file) => file, Err(_) => { eprintln!("Error: Unable to open file"); return; } }; let mut word_count: HashMap<String, u32> = HashMap::new(); let reader = BufReader::new(file); for line in reader.lines() { let line = match line { Ok(line) => line, Err(_) => continue, }; let words = line.split_whitespace(); for word in words { *word_count.entry(word.to_string()).or_insert(0) += 1; } } print_json_output(&word_count); } fn print_json_output(word_count: &HashMap<String, u32>) { print!("{{"); let mut first = true; for (word, count) in word_count { if !first { print!(", "); } print!("\"{}\": {}", word, count); first = false; } println!("}}"); } |
{"we": 4, "was": 10, "all": 2, "other": 1, "the": 11, "Heaven,": 1, "incredulity,": 1, "age": 2, "before": 2, "nothing": 1, "despair,": 1, "hope,": 1, "of": 10, "us,": 2, "were": 2, "times,": 2, "epoch": 2, "foolishness,": 1, "wisdom,": 1, "it": 10, "going": 2, "direct": 2, "Light,": 1, "belief,": 1, "season": 2, "to": 1, "way": 1, "winter": 1, "had": 2, "spring": 1, "best": 1, "everything": 1, "worst": 1, "Darkness,": 1}
I extracted the following JSON from that output:
1 | {"we": 4, "was": 10, "all": 2, "other": 1, "the": 11, "Heaven,": 1, "incredulity,": 1, "age": 2, "before": 2, "nothing": 1, "despair,": 1, "hope,": 1, "of": 10, "us,": 2, "were": 2, "times,": 2, "epoch": 2, "foolishness,": 1, "wisdom,": 1, "it": 10, "going": 2, "direct": 2, "Light,": 1, "belief,": 1, "season": 2, "to": 1, "way": 1, "winter": 1, "had": 2, "spring": 1, "best": 1, "everything": 1, "worst": 1, "Darkness,": 1} |
Testing if the previous output matches the JSON: {
"the": 11,
"it": 10,
"was": 10,
"of": 10,
"we": 4,
"times,": 2,
"age": 2,
"epoch": 2,
"season": 2,
"had": 2,
"before": 2,
"us,": 2,
"were": 2,
"all": 2,
"going": 2,
"direct": 2,
"best": 1,
"worst": 1,
"wisdom,": 1,
"foolishness,": 1,
"belief,": 1,
"incredulity,": 1,
"Light,": 1,
"Darkness,": 1,
"spring": 1,
"hope,": 1,
"winter": 1,
"despair,": 1,
"everything": 1,
"nothing": 1,
"to": 1,
"Heaven,": 1,
"other": 1,
"way": 1
}
: True