First, preparatory work
1, the related library
Because most of the red Book is Chinese characters, the standard library is currently not directly processed. So, before parsing, in the library of dependencies in the Toml file, add the following:
encoding = "0.2"
Refer to the following information:
Https://github.com/lifthrasiir/rust-encoding
2, a dream of a red mansion TXT file. Find a mother directly.
Second, the relevant code
extern crate encoding;
Use encoding::{encoding, decodertrap};
Use encoding::all::gb18030; The font that can be converted to Chinese characters
fn main () {let
mut f = file::open ("C:\\users\\desktop\\redstory.txt")
. OK ()
. Expect (" File read error! ");
Let Mut hp:hashmap<char, i32> = Hashmap::new ();
Let Mut reader:vec<u8> = Vec::new ();//u8 set
f.read_to_end (&mut reader). OK (). Expect ("Can not read file"); C9/>let Mut content = string::new ();//String
gb18030.decode_to (&mut Reader, Decodertrap::ignore, &mut content);
For C in Content.chars () {let
counter = Hp.entry (c). Or_insert (0);
*counter + 1;
}
println! ("hp:{:?}", HP);
}
Or main{}, the following code can be changed:
extern crate encoding;
Use encoding::{encoding, decodertrap};
Use encoding::all::gb18030; The font that can be converted to Chinese characters
fn main () {let
mut f = file::open ("C:\\users\\desktop\\redstory.txt")
. OK ()
. Expect (" File read error! ");
Let Mut reader:vec<u8> = Vec::new ();
Let Mut hp:hashmap<char, i32> = Hashmap::new ();
F.read_to_end (&mut Reader). OK (). Expect ("Can not read file");
Let content:string = Gb18030.decode (&reader, decodertrap::strict). Unwrap ();
For d in Content.chars () {let
counter = Hp.entry (d). Or_insert (0);
*counter + 1;
}
println! ("HP: {:?}", HP);
}
You can see how often the different strings appear.