How do I use the SHIFT-JIS encoding in Rust?

吃可爱长大的小学妹 提交于 2019-12-08 07:46:41

问题


According to this Github issue, the rust-encoding crate is missing SHIFT-JIS support. What's the best way to decode SHIFT-JIS in Rust in light of this?


回答1:


encoding_rs::SHIFT_JIS, a crate made for Firefox, can be used instead! :)

extern crate encoding_rs;
use encoding_rs::SHIFT_JIS;

fn main() {
    let data = vec![142,75,130,209,130,189,142,169,147,93,142,212,130,198,141,98,138,107,151,222];
    let (res, _enc, errors) = SHIFT_JIS.decode(&data);
    if errors {
        eprintln!("Failed");
    } else {
        println!("{}", res);
    }   
}

Outputs:

錆びた自転車と甲殻類

Note that res is a Cow<'_, str> - you may need to use into_owned() depending on your use case.



来源:https://stackoverflow.com/questions/48136939/how-do-i-use-the-shift-jis-encoding-in-rust

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!