Fredrick Brennan
Fredrick Brennan

Reputation: 7357

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

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?

Upvotes: 4

Views: 2312

Answers (1)

Fredrick Brennan
Fredrick Brennan

Reputation: 7357

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.

Upvotes: 5

Related Questions