Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 722
Fredrick Brennan Avatar asked Oct 22 '25 16:10

Fredrick Brennan


1 Answers

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.

like image 131
Fredrick Brennan Avatar answered Oct 25 '25 15:10

Fredrick Brennan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!