"encoding_rs": how to specify the encoding as "String"?

Hi all
(I'm a beginner with Rust)

This works perfectly:

use encoding_rs;
use encoding_rs::SHIFT_JIS;

let raw_data = vec![142,75,130,209,130,189,142,169,147,93,142,212,130,198,141,98,138,107,151,222];
let (result, _encoding, errors) = SHIFT_JIS.decode(&raw_data);
println!("OUTPUT \"{}\"", result);

The console/terminal prints this, which is absolutely OK:

OUTPUT "錆びた自転車と甲殻類"

My question is, how can I specify the encoding ("SHIFT_JIS" in the above example) as a string with "encoding_rs" when calling the "decode()"-method?
Alterntively, can I somehow create an "encoding"-variable (like the "SHIFT_JIS" above) by specifying as a String its type when I create it?

In Python I used to execute...
<my_bytes_variable>.decode(sCharset, errors="strict")
...where "sCharset" (ok, maybe I should have called it "sEncoding" - I still don't fully understand the difference between charset and encoding) contained e.g. the string "shift_jis" or anything else.

I can think that it's something simple that I overlooked but after 10 hours of digging into it I start getting desperate... :stuck_out_tongue:

Cheers

Umm... Have you tried to look on the documentation?

That's not an insult. When I have only started using Rust I was, often, confused by the lack of answers even to very simple questions even for very popular crates on StackOverflow and other sites. Then I tried to rely on the documentation and compiler more… and realized why.

I mean: if you read the documentation and you first attempt to use crate according to it works… there would be no trail for Google to find, right?

Read the documentation for crates, it's your friends.

This works for me:

use encoding_rs;

fn main() {
  let raw_data = vec![142,75,130,209,130,189,142,169,147,93,142,212,130,198,141,98,138,107,151,222];
  if let Some(encoding) = encoding_rs::Encoding::for_label(b"SHIFT_JIS") {
    let (result, _encoding, _errors) = encoding.decode(&raw_data);
    println!("OUTPUT \"{}\"", result);
  }
}
1 Like

Dammit thanks a lot - I don't know, I somehow completely overlooked that (maybe because of the "for_label" which was kind of cryptic for me ("for" what?)).
Thank you!!! :smiley: :smiley:

Yes, sometimes a Rust method name sounds strange by itself, when it is meant to be read in context. Seeing a function name for_label by itself I quite agree that it looks a bit peculiar, but reading Encoding::for_label(some_label) in the code it is extremely clear and readable. I don't think I have seen that pattern much in other languages, so it might take some getting used to when new to Rust.

It's called fluent interface and is quite popular in many languages.

Guido van Rossum doesn't like it, though, thus it's rarely seen in Python.

Yes, large part of the chainging pattern are common with other languages, it's just the naming style of static methods / named constructors that I think is unusual (but good).

His dislike is for chaining in general. I strongly disagree with him (and agree with most Rust developers, it seems) as I think such chains are often the most readable way to express something. But I do admit that in a language where anything can be null it can make it harder to realize whith step in the chain caused a null pointer exception, and in a language without static typing you never really known if the methods are going to exist. Since Python has booth of these caracteristics, I can see his point. But Rust does not have any of those problems. :sunglasses:

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.