如何在Rust中使用SHIFT-JIS编码? [英] How do I use the SHIFT-JIS encoding in Rust?

查看:165
本文介绍了如何在Rust中使用SHIFT-JIS编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此Github问题,缺少了锈编码板条箱SHIFT-JIS支持.鉴于此,在Rust中解码SHIFT-JIS的最佳方法是什么?

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?

推荐答案

encoding_rs::SHIFT_JIS ,这是为Firefox创建的板条箱! :)

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);
    }   
}

输出:

錆びた自転車と甲殻類

请注意,resCow<'_, str>-根据使用情况,您可能需要使用into_owned().

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

这篇关于如何在Rust中使用SHIFT-JIS编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆