Rust: A statistical _rust of characters in the Book of the Dream of Red mansions

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.