Rust: 如何读写中文字符?

用Rust进行IO时,往往会碰到中文字符,用标准库的方法是有问题的。那如何办?

一、外部库 encoding

在toml文件[dependencies]下加入:

encoding = “0.2”

二、有中文字符下的读和写

1、没有中文字符,比如全是ASCII码字符下可以,按标准库下写
// 读

use std::io;
use std::io::prelude::*;
use std::fs::File;

fn main() -> io::Result<()> {
    let mut f = File::open("foo.txt")?;
    let mut buffer = [0; 10];

    // read up to 10 bytes
    f.read(&mut buffer)?;

    let mut buffer = Vec::new();
    // read the whole file
    f.read_to_end(&mut buffer)?;

    // read into a String, so that you don't need to do the conversion.
    let mut buffer = String::new();
    f.read_to_string(&mut buffer)?;

    // and more! See the other methods for more details.
    Ok(())
}

//写

use std::io::prelude::*;
use std::fs::File;

fn main() -> std::io::Result<()> {
    let mut buffer = File::create("foo.txt")?;
    buffer.write_all(b"some bytes")?;
    Ok(())
}

2、有中文字符下,上面是方法是行不通的。

extern crate encoding;
use encoding::all::GB18030;
use encoding::{DecoderTrap, EncoderTrap, Encoding};
// read
fn file_read(path: &str) -> io::Result {
    let mut f = File::open(path)?;
    let mut reader: Vec = Vec::new();
    f.read_to_end(&mut reader).ok().expect("can not read file");
    let content: String = GB18030.decode(&reader, DecoderTrap::Strict).unwrap();
    println!("content:{}", content);
    Ok(content)
}
// write
fn file_write(text: io::Result>, _path: &str) -> io::Result<()> {
    let text = text.unwrap();
    let mut buffer = File::create(_path)?;
    for tx in text {
        //let tx_u8: Vec = tx.chars().map(|x| x as u8).collect(); 为什么这样写不行?
        let tx_u8: Vec = GB18030.encode(&tx, EncoderTrap::Strict).unwrap();
        buffer.write_all(&tx_u8)?;
    }
    Ok(())
}

你可能感兴趣的:(Rust)