Letter frequency: Difference between revisions

→‎{{header|Run BASIC}}: add Rust solution
(→‎{{header|Run BASIC}}: add Rust solution)
Line 2,145:
Ascii:117 char:u Count: 16 3.4%
Ascii:120 char:x Count: 7 1.5%
</pre>
=={{header|Rust}}==
This program works with rust 0.11-pre. It uses hashmap from collections library to gather statistics. Works fine with utf-8 characters.
<lang Rust>extern crate collections;
 
use std::io::fs::File;
use std::io::BufferedReader;
use std::os;
 
fn main() {
let filename = match os::args().len() {
1 => fail!("You must enter a filename to read line by line"),
_ => os::args()[1]
};
 
let file = File::open(&Path::new(filename));
let mut reader = BufferedReader::new(file);
 
let mut s : collections::HashMap<char,uint> = collections::HashMap::new();
 
for c in reader.chars() {
let letter = c.unwrap();
let counter = match s.contains_key(&letter) {
true => s.get(&letter) + 1,
false => 1,
};
s.insert(letter, counter);
}
 
println!("{}", s);
}</lang>
 
Sample output on the source code of the program itself (lines split for readability):<pre>
{=: 10, 1: 4, g: 4, y: 3, f: 15, o: 21, &: 3, !: 2, /: 6, w: 5, h: 8, +: 1, m: 11,
r: 31, : 164, [: 1, a: 27, .: 7, x: 1, c: 18, q: 1, k: 2, L: 1, ,: 6, {: 5, }: 5,
:: 30, I: 1, F: 2, Y: 1, P: 1, ]: 1, d: 11, n: 29, ": 4, u: 15, l: 30, B: 2, ): 15,
_: 3,
: 30, <: 1, t: 45, R: 2, (: 15, i: 23, b: 1, H: 2, p: 7, M: 2, ;: 12, >: 5, s: 29, e: 70}
</pre>
 
Anonymous user